1

I have a large data frame and an aggregation of it - in the aggregated data frame I want to add a column with the resepctive number of rows that contain the matching expression in the original data frame, e.g.

df1<-data.frame(A = c("a","b","a","a","b","c"), B = 1:12)
df2<-aggregate(df1$B, by=list(category=df1$A), FUN=sum)

and in this aggregated data frame I would like to add a column 'count' (referring to the original data frame), i.e.

  category  x count
1        a 34    6
2        b 26    4
3        c 18    2 

Among many other attempts I tried

df2$count<-nrow(df1[df1$A == df2$category,])

which of course didn't work...

Any help would be appreciated! Thanks!

Don Joe
  • 13
  • 3

1 Answers1

0

Using tidyverse, this can be done more easily. After grouping by 'A', get the sum of 'B' and count with n()

library(dplyr)
df1 %>% 
    group_by(category = A) %>%
    summarise(x = sum(B), count = n())
akrun
  • 874,273
  • 37
  • 540
  • 662