a barking spider
a barking spider

Reputation: 633

How do I tabulate across an entire R data frame?

I've got a data frame containing, say, the columns v1, v2, ..., v30 which contain the same factor variables, although the levels are different from column to column ('210N' under v3 might not appear under v5, for example - I'm not sure whether that is contributing to my trouble). Ideally, I'm hoping that, rather than doing a table(df$v1), table(df$v2), ... and onward to table(df$v30), and adding up all the counts of the value I'm interested in, there's some kind of solution out there that performs the equivalent of table(df$[, v1...v30]) - without requiring me to enforce the same levels across all columns, as in theory there should be around ~6000 levels in all.

So far, I've naively tried to merge a table on v1 with a table on v2, with all=TRUE set, although that doesn't do the trick: the the ensuing table contains two different counts for the same factor level, i.e, '210N' appears twice, but with different frequencies.

Upvotes: 1

Views: 1615

Answers (2)

MYaseen208
MYaseen208

Reputation: 23898

One approach is

library(plyr)
llply(.data=df, .fun=table)

Upvotes: 3

Ramnath
Ramnath

Reputation: 55695

Here is one approach to do this

table(unlist(df))

Upvotes: 4

Related Questions