Skip to content
Prev 299457 / 398503 Next

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

Hello everyone,

I have a dataframe with 1 column and I'd like to replace that column
with a moving average.
Example:
[1]  1  2  3  4  5  6  7  8  9 10
V1
1   1
2   2
3   3
4   4
5   5
6   6
7   7
8   8
9   9
10 10
Error in `$<-.data.frame`(`*tmp*`, "V1", value = c(2, 3, 4, 5, 6, 7, 8,  :
  replacement has 8 rows, data has 10
I could use a temporary variable to store the results of rollapply()
and then reconstruct the data frame, but I was wondering if there is a
one-liner that can achieve the same thing.

Best regards,
Stathis

P.S. If you don't mind, cc me at your reply because I'm not subscribed
to the list (but I will check the archive anyway).