-1

I have a large data frame with first 2 columns as follows. I managed to group_count but unable to get a column with total count. i.e 14

id  company group_count
1   aaa 1
1   bbb 2
1   bbb 2
1   ccc 1
1   ddd 1
1   eee 2
1   eee 2
1   fff 3
1   fff 3
1   fff 3
1   ggg 1
1   hhh 1
1   iii 1
1   jjj 1

I am expecting the following

id  company count   total count
1   aaa 1   10
1   bbb 2   10
1   bbb 2   10
1   ccc 1   10
1   ddd 1   10
1   eee 2   10
1   eee 2   10
1   fff 3   10
1   fff 3   10
1   fff 3   10
1   ggg 1   10
1   hhh 1   10
1   iii 1   10
1   jjj 1   10
Sunita
  • 13
  • 3

1 Answers1

1

Use ave to group company by id and calculate the length of unique elements. We first convert company to factor and then numeric so that the output is also numeric

ave(as.numeric(as.factor(df$company)), df$id, FUN = function(x) length(unique(x)))
# [1] 10 10 10 10 10 10 10 10 10 10 10 10 10 10
d.b
  • 32,245
  • 6
  • 36
  • 77