编辑:我问这个问题很不好。有关更明确的问题,请参阅在dplyr中找到滑动窗口上的方差
我正在尝试使用每一行的值和组的值来调用函数。
# make some data with categories a and b
library(dplyr)
df = expand.grid(
a = LETTERS[1:3],
b = 1:3,
x = 1:5
)
# add a variable that changes within group
df$b2 = df$b + floor(runif(nrow(df))*100)
df %>%
# group the data
group_by(a, b) %>%
# row by row analysis
rowwise() %>%
# do some function based on this row's value and the vector for the group
mutate(y = x + 100*max(.$b2))
我只想.$b2
对应当前组中的项目。而是整个数据帧。
有什么方法可以只获取小组的数据吗?
注意:我实际上并不关心最大 它只是更复杂功能的代表。我需要能够打电话foo(one_value, group_vector)
。
尝试
df %>%
group_by(a,b) %>%
mutate(y=x+100*max(b2))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句