r - Collecting success and total from incomplete binary groups in dplyr -
lets have following
>blob id group growth 1 1 2 1 3 b 0 4 b 1 5 b 0 6 c 0 7 c 0 8 c 0 i pull out success out of total data. have gone far
blob %>% group_by(group,growth) %>% tally() group growth n 1 2 b 0 2 b 1 1 c 0 3 i have like
group success total 2 2 b 1 3 c 0 3 i have tried
sales %>% group_by(group,growth) %>% tally() %>% summarise(fail= n[factor(growth)==1],total = sum(n)) but error because not growths equal 1.
n() function dplyr count number. if group_by group, can use n() count number of rows , use sum add success number.
library(dplyr) dt2 <- dt %>% group_by(group) %>% summarise(success = sum(growth), n = n()) data preparation
dt <- read.table(text = "id group growth 1 1 2 1 3 b 0 4 b 1 5 b 0 6 c 0 7 c 0 8 c 0", header = true, stringsasfactors = false)
Comments
Post a Comment