我正在尝试下一个练习:
data<-tbl_df(data.frame(A=c(6,3,2,4,5,3,3,4),
B=c(10,19,12,10,10,9,8,20),
C=c("A","B","C","A","A","C","C","B"),
D=c("AB","AB","AB","CD","CD","AB","CD","CD")))
data%>%group_by(C,D)%>%
summarise(Frecuencia=n(),suma=sum(A),cumsum(suma))
我正在尝试使用列suma的累加总和来添加新列,但这不起作用。
> data%>%group_by(C,D)%>%
+ summarise(Frecuencia=n(),suma=sum(A),cumsum(suma))
# A tibble: 6 x 5
# Groups: C [?]
C D Frecuencia suma `cumsum(suma)`
<fct> <fct> <int> <dbl> <dbl>
1 A AB 1 6 6
2 A CD 2 9 9
3 B AB 1 3 3
4 B CD 1 4 4
5 C AB 2 5 5
6 C CD 1 3 3
为什么不起作用?
在此示例中尝试使用mutate是可行的,但是在我的真实数据中,我无法获得结果。