I have a dataframe (training.set) that is 150 observations of 83 variables. I want to transform 82 of those columns with some moving averages. The problem is the results end up only being 150 numeric values (i.e. 1 column).
我有一个数据帧(training.set),它是对83个变量的150个观测值。我想用一些移动平均线转换其中82列。问题是结果最终只有150个数值(即1列)。
How would I apply the moving average function across each column individually in the data and keep the 83rd column unchanged? I feel like this is super simple, but I can't find a solution.
如何在数据中单独应用每列的移动平均功能并保持第83列不变?我觉得这很简单,但我找不到解决方案。
My current code
我目前的代码
# apply moving average on training.set data to 82 of 83 rows
library(TTR) #load TTR library for SMA functions
ts.sma <- SMA(training.set[,1:82], n = 10)
ts.sma
Thanks for your help.
谢谢你的帮助。
1 个解决方案
#1
1
apply(training.set[,1:82], 2, SMA, n=10)
Note that this will convert your data.frame to a matrix - wrap it in data.frame(...)
if you need the output to be a data.frame.
请注意,如果您需要将输出作为data.frame,这会将data.frame转换为矩阵 - 将其包装在data.frame(...)中。
#1
1
apply(training.set[,1:82], 2, SMA, n=10)
Note that this will convert your data.frame to a matrix - wrap it in data.frame(...)
if you need the output to be a data.frame.
请注意,如果您需要将输出作为data.frame,这会将data.frame转换为矩阵 - 将其包装在data.frame(...)中。