grapeporcupine
grapeporcupine

Reputation: 77

R - number of rows in which each unique value among certain columns shows up

I have the below abridged dataframe. There are additional columns in between the shown columns that I have not included:

Case Type1 Type2 Type3 Type4
1 A B C NA
2 B A D NA
3 C D A NA
4 D NA NA A
5 E B A NA

I would like to generate another data frame that counts the number of rows in which each unique value among certain columns (the ones shown) show up

Type Number of Cases
A 5
B 3
C 2
D 3
E 1
NA 5

Thank you!

Upvotes: 1

Views: 24

Answers (1)

akrun
akrun

Reputation: 886938

Just unlist the columns and get the table and convert to data.frame if we need a two column data

as.data.frame(table(unlist(df1[-1]), useNA = "always"))

-output

  Var1 Freq
1    A    5
2    B    3
3    C    2
4    D    3
5    E    1
6 <NA>    6

Upvotes: 2

Related Questions