简体   繁体   中英

Count combination of variables based on unique column value

for a df

id=c(12,12,13,14,14,15,16,17,18,18)
reg = c('FR','FR','DE','US','US','TZ','MK','GR','ES','ES')
code1=c('F56','G76','G56','T78','G78','G76','G64','T65','G79','G56')
code2=c('G56','I89','J83','S46','D78','G56','H89','G56','W34','T89')
bin1= c(0,1,1,0,1,1,0,0,0,1)
bin2= c(1,0,1,0,0,1,1,1,0,0)
bin3= c(0,0,0,1,1,0,0,1,0,1)
df = data.frame(idnumber,reg,code1,code2, bin1, bin2, bin3)

looks like

id  reg code1 code2 bin1 bin2 bin3
12  FR  F56   G56    0    1    0
12  FR  G76   I89    1    0    0
13  DE  G56   J83    1    1    0
14  US  T78   S46    0    0    1
14  US  G78   D78    1    0    1
15  TZ  G76   G56    1    1    0
16  MK  G64   H89    0    1    0
17  GR  T65   G56    0    1    1
18  ES  G79   W34    0    0    0
18  ES  G56   T89    1    0    1

I'm trying to count the number if occurrences of a combinations of binary variables ( bin1 , bin2 , bin3 ) values, aggregated by unique idnumber , something like:

bin1 bin2 bin3 count
  1   1    0    3
  1   0    1    2
  0   1    0    1
  0   1    0    1

any suggestion welcomed! Cheers

If I understood you correctly, you aggregate using something like an OR operator and then count the unique values. Since it is all 0 and 1s to start with, you can get the max of each column when separated by id. Try below in dplyr:

library(dplyr)
df %>% 
select(id,bin1,bin2,bin3) %>% 
group_by(id) %>% 
summarise_all(max) %>% 
count(bin1,bin2,bin3)

# A tibble: 4 x 4
   bin1  bin2  bin3     n
  <dbl> <dbl> <dbl> <int>
1     0     1     0     1
2     0     1     1     1
3     1     0     1     2
4     1     1     0     3

Without installing dplyr, you can do this:

by_id = aggregate(df[,c("bin1","bin2","bin3")],list(id=df$id),max)
aggregate(id~bin1+bin2+bin3,by_id,length)
  bin1 bin2 bin3 id
1    0    1    0  1
2    1    1    0  3
3    1    0    1  2
4    0    1    1  1

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