R:如何将移动平均值应用于数据帧中的列子集? [英] R: How to apply moving averages to subset of columns in a data frame?
问题描述
如何在数据中单独应用移动平均函数,并保持第83列不变?我觉得这是超级简单,但我找不到解决方案。
我目前的代码
#应用移动平均值在training.set数据到83行中的82行
库(TTR)#load用于SMA功能的#load TTR库
ts.sma< - SMA(training.set [,1:82],n = 10)
ts.sma
感谢您的帮助
apply(training.set [,1:82],2,SMA,n = 10)
请注意,这将将您的data.frame转换为矩阵 - 将其包装在 data.frame(... )
如果您需要输出为data.frame。
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).
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.
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.
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.
这篇关于R:如何将移动平均值应用于数据帧中的列子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!