考虑下表:
df$product <- df$a * df$b
假设我想在表格中添加一列,该列是a和b列的乘积。
我可以做do()
。如果我想使用df %>% group_by(grp) %>% do({
d <- .
d$product <- d$a * d$b
d #expression returns the data frame, d
})
#result is as expected
grp a b product
<fctr> <int> <int> <int>
1 a 1 6 6
2 b 2 7 14
3 c 3 8 24
4 d 4 9 36
5 e 5 10 50
以下作品:
.
但是当我分配给df %>% group_by(grp) %>% do({
.$product <- .$a * .$b
.
})
#The output only includes data from row 1 (grp==a) of the input
grp a b product
<fctr> <int> <int> <int>
1 a 1 6 6
2 a 1 6 6
3 a 1 6 6
4 a 1 6 6
5 a 1 6 6
时,它无法正常工作:
greaterThanOrEqualTo