Reputation: 407
I am trying to do the next exercise:
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))
I am trying to add a new column with the cumulative sum of the column suma, but It doesn´t works.
> 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
Why is not it working?
Trying with mutate in this example works, but in my real data I can´t get the result.
Upvotes: 1
Views: 2080
Reputation: 1210
I think you cannot create a new column with a column that is being created by a function. Alternatively, you can use mutate
function and add a new column keeping the existing columns.
This code cumsums
based on each value of column C
. If you can provide the desired output, I can answer your questions more accurate.
Code:
data %>% group_by(C,D) %>%
summarise(Frecuencia=n(),suma=sum(A)) %>%
mutate(cumsum(suma))
Output:
Upvotes: 2