I need to calculate the moving amount of a column row in a data frame in which there is a condition. The data that I have have several observations for sku. I want to calculate the sum of 5 consecutive lines for each value of "sku". In the event that I get to the stage when I do not have 5 consecutive observations for "sku", we would summarize the remaining observations of the string for this value.
As an illustrative example, consider the following data frame:
data <- structure(list(sku = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L), tf = c(50.79, 46.39, 47.85, 45.79, 44.46, 49.99,
46.12, 44.4, 41.21, 53.7, 53.9, 44.91, 59.64, 41.96, 52.26, 43.48,
46.93, 51.2, 54.31, 42.5, 47.2, 57.54, 63.23, 48.98, 52.38, 59.9,
53.01, 50.35, 41.86, 46.42)), .Names = c("sku", "tf"), row.names = c(NA,
-30L), class = "data.frame")
In this data frame, we want to summarize the "tf" variable for the 5 rolling values โโof each "sku" value.
We were able to accomplish this using the following code:
data[,c("day_5")]<-unlist(mapply(function(y){
end1<-(which(data$sku==unique(data$sku)[y]))[length(which(data$sku==unique(data$sku)[y]))]
start<-(which(data$sku==unique(data$sku)[y]))[1]
d<-data$tf[start:end1]
r<-mapply(function(x){if (x+4 <= length(d)) {sum(d[x:(x+4)])} else {sum(d[x:length(d)])}},1:length(d))
},1:length(unique(data$sku))))
"day_5" , , , "sku".
- , ?