R:对组应用函数 [英] R: applying a function over a group
问题描述
我希望将函数应用于数据框,然后将该函数的结果存储在数据框的新列中.
I am looking to apply a function to a data frame and then store the results of that function in a new column in the data frame.
这是我的数据框tradeData的示例:
Here is a sample of my data frame, tradeData:
Login AL Diff
a 1 0
a 1 0
a 1 0
a 0 1
a 0 0
a 0 0
a 0 0
a 1 -1
a 1 0
a 0 1
a 1 -1
a 1 0
a 0 1
b 1 0
b 0 1
b 0 0
b 0 0
b 1 -1
c 1 0
c 1 0
c 0 1
c 0 0
c 1 -1
其中差异"列是我要添加的列.它只是 tradeData 的 row(x-1) 和 row(x) 值之间的差异,按登录分组.
Where the "Diff" column is the column I am trying to add. It just just the difference between the values row(x-1) and row(x) of tradeData, grouped by Login.
以下是我尝试过的一些示例:
Here are some samples of what I've tried:
tradeData$Diff = ave(tradeData$AL,tradeData$Login,FUN = function(x) {diff(x)})
和
tradeData$Diff = as.data.frame(with(tradeData,tapply(AL,Login,FUN = diff)))
到目前为止,我发现以下问题很有用:R 将函数应用于数据框的子集,但我不确定如何从这里开始,因为我不断收到错误.
I've found the following question useful thus far: R applying a function to a subset of a data frame but I am unsure how to proceed from here, as I keep getting errors.
谢谢
推荐答案
你可以试试
with(tradeData, ave(AL, Login, FUN=function(x) -1*c(0, diff(x))))
#[1] 0 0 0 1 0 0 0 -1 0 1 -1 0 1 0 1 0 0 -1 0 0 1 0 -1
或者使用 data.table
的选项.使用 setDT
将data.frame"转换为data.table".按组计算当前值和下一个值之间的差异(by=Login
).shift
函数(在新的 devel 版本中引入)type
等于lead"获取下一个值.
Or an option using data.table
. Convert the "data.frame" to "data.table" with setDT
. Take the difference between current and next value by group (by=Login
). The shift
function (introduced in the new devel version) with type
equals "lead" gets the next value.
library(data.table)#data.table_1.9.5
setDT(tradeData)[, Diff:=AL-shift(AL, type='lead',
fill=0) , by=Login][]
这篇关于R:对组应用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!