costebk08
costebk08

Reputation: 1359

Numerical frequency counts in R

I have a large data frame (100 columns and ~250,000 observations) of which I am trying to obtain the frequency counts of the numerical values and the NAs for each variable simultaneously. I have tried to use the table but the data is too big for the function. Any suggestions for how to gain these counts? The data only contains NAs and the numbers 1 through 5, so for example a small glimpse of the data is:

i1 i2 i3 i4 i5 i6 i7 i8
5  4  3  NA 3  4  3  NA
4  3  2  5  4  3  5  NA

Thank you!

Upvotes: 0

Views: 465

Answers (1)

Adam Birenbaum
Adam Birenbaum

Reputation: 950

I created a dataframe with 5 columns and 250,000 rows, each column was a sample drawn from the vector c(1:5,NA). I had no problem using the apply function like so:

new_df <- apply(df,2,table)

I'm assuming you wanted the frequencies broken up by column. If you actually wanted the frequency over the entire data frame, you could do the mentioned apply function over the columns and then another apply function to sum the rows like so:

apply(new_df,1,sum)

my result looked like this

     1      2      3      4      5 
167025 166677 166471 166849 166541 

Upvotes: 1

Related Questions