I have a data frame of daily data: df with four columns: Date, A, B, C
and am trying to get the mean of weekly data by column.
I tried:
library(xts)
xdf <- xts(df[ ,-1],as.Date(df$Date))
apply.weekly(xdf,mean)
but this gives me the mean of all the columns. I need the weekly means of each column.
Thank you for your help
First get some data (I’ll use a
data.framesince that’s what you’re using, but I really suggest you use a time series class for time series objects)apply.weeklywill split the data up by weeks and apply a function to each week.apply.weekly(SPY, mean)would calculate the mean of all the data for that week, but you want it to calculate the mean of each column for each week. So, you have toapplymeanto each columnFor reference,
apply.weeklyis a wrapper forperiod.applyso you could also get the above result with