Skip to content
Prev 299465 / 398503 Next

How to replace a column in a data frame with another one with a different size

On Jul 8, 2012, at 9:31 AM, Stathis Kamperis <ekamperi at gmail.com> wrote:

            
I'm not sure you need the outer df[...] -- I think you just want

df$V1 <- rollapply(df$V1,3,mean)

However, this will still give you the error message you're seeing because rollapply() only returns 8 values here (you don't get the "endpoints" by default). To get the right number of rows, you want

rollapply(df$V1, 3, mean, fill = NA) # Change NA if desired

which will put NA's on each end and give you a length 10 result, as needed. 

Best, 
Michael