ct09
ct09

Reputation: 37

Calculate running average in R using first 50 rows

I want to calculate running average of 6th column of my data with a window of 50 and store the averages in the 7th column(Volume) starting from row 50(first 49 rows would be blank or NA) So far i have referred the below code which stores the values in the first row of 7th column(Volume). How can i change this to start from row 50?

for(i in 1: length(StockData[ , 6]-49)) 
{  
  StockData$Volume [i] <- mean(StockData[i:min(i+49, nrow(StockData)),6]) 
}

Upvotes: 1

Views: 222

Answers (1)

akrun
akrun

Reputation: 887203

Once we have the index to loop from the 1st to the 49 rows before the last row(i1), assign the mean to the 'Volume' column by adding each of the index in for loop with 49 i.e. 1 + 49 = 50 2 + 49 = 51 etc.

i1 <- 1:(nrow(StockData) - 49)
for(i in i1) StockData$Volume[i + 49] <- mean(StockData[[6]][i:(i+ 49)])
StockData$Volume
#  [1]          NA          NA          NA          NA          NA          NA          NA
#  [8]          NA          NA          NA          NA          NA          NA          NA
# [15]          NA          NA          NA          NA          NA          NA          NA
# [22]          NA          NA          NA          NA          NA          NA          NA
# [29]          NA          NA          NA          NA          NA          NA          NA
# [36]          NA          NA          NA          NA          NA          NA          NA
# [43]          NA          NA          NA          NA          NA          NA          NA
# [50] -0.19629964 -0.24644362 -0.23783891 -0.26612198 -0.24675588 -0.22167644 -0.19686068
# [57] -0.17468898 -0.22060983 -0.24581253 -0.28938032 -0.25877811 -0.23004419 -0.21873832
# [64] -0.25380578 -0.29241801 -0.27468023 -0.26958748 -0.23289877 -0.20673662 -0.19514563
# [71] -0.20304788 -0.25285171 -0.27237608 -0.28292086 -0.27282226 -0.28069595 -0.26402249
# [78] -0.24157390 -0.20787905 -0.18768505 -0.20224311 -0.20345300 -0.19355069 -0.19980993
# [85] -0.21982236 -0.21592697 -0.19056489 -0.18937285 -0.25814298 -0.24351155 -0.22539465
# [92] -0.18168959 -0.20872355 -0.19644329 -0.17326079 -0.13079761 -0.08915261 -0.10408647
# [99] -0.07350988 -0.01842930

It would be the same values if we use rollmean from zoo

library(zoo)
rollmean(StockData[[6]], k = 50, fill = NA, align = "right")
# [1]          NA          NA          NA          NA          NA          NA          NA
#  [8]          NA          NA          NA          NA          NA          NA          NA
# [15]          NA          NA          NA          NA          NA          NA          NA
# [22]          NA          NA          NA          NA          NA          NA          NA
# [29]          NA          NA          NA          NA          NA          NA          NA
# [36]          NA          NA          NA          NA          NA          NA          NA
# [43]          NA          NA          NA          NA          NA          NA          NA
# [50] -0.19629964 -0.24644362 -0.23783891 -0.26612198 -0.24675588 -0.22167644 -0.19686068
# [57] -0.17468898 -0.22060983 -0.24581253 -0.28938032 -0.25877811 -0.23004419 -0.21873832
# [64] -0.25380578 -0.29241801 -0.27468023 -0.26958748 -0.23289877 -0.20673662 -0.19514563
# [71] -0.20304788 -0.25285171 -0.27237608 -0.28292086 -0.27282226 -0.28069595 -0.26402249
# [78] -0.24157390 -0.20787905 -0.18768505 -0.20224311 -0.20345300 -0.19355069 -0.19980993
# [85] -0.21982236 -0.21592697 -0.19056489 -0.18937285 -0.25814298 -0.24351155 -0.22539465
# [92] -0.18168959 -0.20872355 -0.19644329 -0.17326079 -0.13079761 -0.08915261 -0.10408647
# [99] -0.07350988 -0.01842930

data

set.seed(24)
StockData <- as.data.frame(matrix(rnorm(100 * 7), ncol =7, nrow = 100, 
    dimnames = list(NULL, c(paste0("V", 1:6), "Volume"))))
StockData$Volume <- NA

Upvotes: 1

Related Questions