How can I transform columns into a sum conditional on the values in another numeric column?

∥☆過路亽.° 提交于 2021-02-17 05:17:08

问题


I have a dataframe designed to compare tickets between two years.

df<-data.frame(year=c("1","2","1","2","1","2"),
           Daysleft=c(8,8,7,7,6,6),
           Count_TypeA=c(1,1,1,1,1,1),
           Count_TypeB=c(5,2,5,2,5,2))

In this example data frame, days left indicates days remaining until the event. What I'd like to do is come up with the cumulative counts where Daysleft >= the Daysleft value, grouped by year. The desired output is below:

desired<-data.frame(year=c("1","2","1","2","1","2"),
                Daysleft=c(8,8,7,7,6,6),
                Count_TypeA=c(1,1,2,2,3,3),
                Count_TypeB=c(5,2,10,4,15,6))

In my real data, there are many Count_Type variables, so ideally I'm hoping for an answer that allows me to avoid naming all the columns using their typed names.

For a quick view of desired output:

  year Daysleft Count_TypeA Count_TypeB
    1        8           1           5
    2        8           1           2
    1        7           2          10
    2        7           2           4
    1        6           3          15
    2        6           3           6

I am guessing there is a trick with dplyr's group_by() that could help, but answers found for similar questions (like Using dplyr to get cumulative count by group) aren't clear to me.

df2<-df %>%
   group_by(year)%>%

回答1:


If you need to do each column independently, then you can use:

df %>% group_by(year) %>% mutate_at(vars(-year, -Daysleft),cumsum)



回答2:


I think the cumsum function works here.

desired <- df %>% group_by(year) %>% mutate(Count_TypeB = cumsum(Count_TypeB)) %>% mutate(Count_TypeA = cumsum(Count_TypeA))


来源:https://stackoverflow.com/questions/59311975/how-can-i-transform-columns-into-a-sum-conditional-on-the-values-in-another-nume

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!