Rooz
Rooz

Reputation: 519

How to count frequency of a categorical variable (level) for each row in a dataframe

I would like to summarize the frequency of each level (category) for each ID in my data frame. For example, how could I generate the values 1,2,0 respectively for the ID 4003491503?

I tried tapply and count and I keep getting errors.

    RespondentID   Case.A    Case.B    Case.C   Freq Red    Freq Blue   Freq Missing/NA
1     4003491503    Red      Blue      Blue      <b> 1            2          0 </b>
2     4003491653    Blue     Red       Red   
3     4003491982    Red      Blue      Red   
4     4003494862    Red      Red        NA   
15    4003494880   Blue     Blue      Blue    

Upvotes: 2

Views: 937

Answers (1)

akrun
akrun

Reputation: 887961

We can melt the dataset with 'id.var' as the 'RespondentID', get the frequency with table convert the output to a data.frame, change the column names and cbind with the original dataset.

library(reshape2)
df2 <- as.data.frame.matrix(table(melt(df1, id.var='RespondentID')[-2], useNA='ifany'))
colnames(df2) <- paste0('Freq', colnames(df2))
cbind(df1, df2)
#   RespondentID Case.A Case.B Case.C FreqBlue FreqRed FreqNA
#1    4003491503    Red   Blue   Blue        2       1      0
#2    4003491653   Blue    Red    Red        1       2      0
#3    4003491982    Red   Blue    Red        1       2      0
#4    4003494862    Red    Red   <NA>        0       2      1
#15   4003494880   Blue   Blue   Blue        3       0      0

Upvotes: 2

Related Questions