Summary count by multiple groups with condition in dplyr

前端 未结 2 1726
耶瑟儿~
耶瑟儿~ 2021-01-22 14:42

I have a dataframe that looks like this:

data <- data.frame(a=c(1,1,0,0,0,0,1,1,1, 0), 
               b=c(\"x\",\"x\",\"x\",\"x\",\"x\",\"y\",\"y\",\"y\",\"z         


        
2条回答
  •  礼貌的吻别
    2021-01-22 15:25

    We can use

    library(dplyr) 
    data %>%
         group_by(d, b) %>% 
         summarise(e = sum(c[a==1], na.rm = TRUE))
    

    Or

    data %>%
          group_by(d, b) %>% 
          summarise(e = sum((a==1)*c, na.rm = TRUE))
    

提交回复
热议问题