Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count of values across multiple columns in R

Tags:

r

count

I have a dataset with a set of values dispersed over multiple columns:

ID Priority 1 Priority 2 Priority 3
2308 Writing Reading Reading
0329 Reading Communication Writing
2389 Communication Writing Communication
2934 Writing Communication Writing

And I would like the output to be a table where the first column are the unique values found (Writing, Reading, Communication) and the rest of the columns are the priorities (Priority 1, Priority 2, Priority 3). In each column should be the count of the priority witin that instance. The output should look like:

Priority Type Priority 1 Priority 2 Priority 3
Writing 2 1 2
Reading 1 1 1
Communication 1 2 1

In my actual dataset, there are many priorities so if possible could we include 1:n for columns?

Thank you in advance.

*Edit Table has been updated with new column for clarification. I would like to ignore the ID column completely and only have a count of the priorities in each of the priorities columns.

like image 989
kioti Avatar asked Dec 31 '25 17:12

kioti


2 Answers

table(stack(df))

               ind
values          Priority 1 Priority 2 Priority 3
  Communication          1          2          1
  Reading                1          1          1
  Writing                2          1          2

If you want it as a dataframe:

as.data.frame.matrix(table(stack(df)))
              Priority 1 Priority 2 Priority 3
Communication          1          2          1
Reading                1          1          1
Writing                2          1          2
like image 147
KU99 Avatar answered Jan 02 '26 09:01

KU99


Try sapply

sapply( dat, table )

              Priority1 Priority2 Priority3
Communication         1         2         1
Reading               1         1         1
Writing               2         1         2
like image 39
Andre Wildberg Avatar answered Jan 02 '26 08:01

Andre Wildberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!