I have a list of Likert values, the values range from 1 to 5. Each possible response may occur once, more than once or not at all per column. I have several columns and rows, each row corresponds to a participant, each column to a question. There is no NA data.
Example:
c1 | c2 | c3 |
---|---|---|
1 | 1 | 5 |
2 | 2 | 5 |
3 | 3 | 4 |
3 | 4 | 3 |
2 | 5 | 1 |
1 | 3 | 1 |
1 | 5 | 1 |
The goal is to count the frequencies of the answer options column wise, to consequently compare them.
So the resulting table should look like this:
- | c1 | c2 | c3 |
---|---|---|---|
1 | 3 | 1 | 3 |
2 | 2 | 1 | 0 |
3 | 2 | 2 | 1 |
4 | 0 | 1 | 1 |
5 | 0 | 2 | 2 |
I know how to do this for one column, and I can look at the frequencies with apply(ds, 1, table)
, but I do not manage to put this into a table to work further with.
Thanks!
This should do it, using plyr
:
count_df = setNames(data.frame(t(plyr::ldply(apply(df, 2, table), rbind)[2:6])), colnames(df))
count_df[is.na(count_df)] = 0