Search code examples
rdataframegroup-bycountfrequency

How to count and group multiple columns in R dataframe?


really basic question... I have a dataframe like the one below, where the numbers indicate a score:

df<-data.frame(A=c(1,2,1,1,3,3,2,2),B=c(2,2,2,3,2,3,3,1),C=c(1,1,1,1,1,2,2,3))

enter image description here

And I would like to change it to this format to plot it in a stacked bar chart:

enter image description here

I know how to do it in a very roundabout and probably overly complicated way, and any suggestions on a more "streamlined" way to do it would be very welcome! Thanks in advance!


Solution

  • The dplyr solutions are likely more scalable, but an alternative base R approach: use do.call along with lapply and table then put it all in a data.frame:

    data.frame(Name = rep(c("A", "B", "C"), each = 3),
          Score = rep(1:3, each = 3),
          Frequency = do.call(c, lapply(df[], table)))
    
    #     Name Score Frequency
    # A.1    A     1         3
    # A.2    A     1         3
    # A.3    A     1         2
    # B.1    B     2         1
    # B.2    B     2         4
    # B.3    B     2         3
    # C.1    C     3         5
    # C.2    C     3         2
    # C.3    C     3         1