简体   繁体   中英

R: Is there a function to aggregate same factors into different groups?

I am trying to build a summary table for my homework. The variable I am using to aggregate is a binary variable (0/1).

total <-aggregate(result ~sex, data=x,sum)

However, I want aggregate every few 0s and every few 1s into different groups. For example:

Sex= 1 1 1 1 0 0 1 1
result = 5 1 10 6 7 8 3 2 

The table I want to get will be Sex 1 result 22, Sex 0 result 15, sex 1 result 5. How can I do this? Any ideas? Thanks!

We can use rleid from data.table to do the grouping and then get the sum of 'result'

library(data.table)
setDT(x)[, .(Sex = Sex[1L],result = sum(result)), by = .(grp= rleid(Sex))][, grp := NULL][]
#    Sex result
#1:   1     22
#2:   0     15
#3:   1      5

Or using dplyr

library(dplyr)
x %>% 
  group_by(grp = cumsum(Sex!= lag(Sex, default = Sex[1]))+1) %>% 
  summarise(Sex = first(Sex), result = sum(result)) %>%
  select(-grp)
#   Sex result
#   <dbl>  <dbl>
#1     1     22
#2     0     15 
#3     1      5

This can also be used with aggregate

aggregate(result ~cbind(Sex = cumsum(c(1, diff(Sex) != 0))), x, sum)

EDIT: Changed the grouping variable in aggregate from rleid(Sex) to one of the options showed in the link provided by @Sotos

data

x <- structure(list(Sex = c(1, 1, 1, 1, 0, 0, 1, 1), result = c(5, 
1, 10, 6, 7, 8, 3, 2)), .Names = c("Sex", "result"), row.names = c(NA, 
-8L), class = "data.frame")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM