JORIS
JORIS

Reputation: 59

R- count values in data.frame

df <- data.frame(row.names = c('ID1','ID2','ID3','ID4'),var1 = c(0,1,2,3),var2 = c(0,0,0,0),var3 = c(1,2,3,0),var4 = c('1','1','2','2'))
> df
    var1 var2 var3 var4
ID1    0    0    1    1
ID2    1    0    2    1
ID3    2    0    3    2
ID4    3    0    0    2

I want df to look like this

   var1  var2  var3  var4
0   1     4     1     0
1   1     0     1     2
2   1     0     1     2
3   1     0     1     0

So I want the values of df to be counted. The problem is, that not every value occurs in every column. I tried this lapply(df,table) but that returns a list which I cannot convert into a data.frame (because of said reason). I could do it kind of manually with table(df$var1) and bind everything together after doing that with every var, but that is boring. Can you find a better way?

Thanks ;)

Upvotes: 1

Views: 225

Answers (3)

Haraldur Karlsson
Haraldur Karlsson

Reputation: 11

To count the total number of values (irrespective of what they are) in a data.frame:

df |> as_vector() |> length()

Upvotes: 0

akrun
akrun

Reputation: 886938

We could do this without any loop

table(c(col(df)), unlist(df))

#    0 1 2 3
#   1 1 1 1 1
#   2 4 0 0 0
#   3 1 1 1 1
#   4 0 2 2 0

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388807

Call table function with factor levels which are present in the entire dataset.

sapply(df,function(x) table(factor(x, levels = 0:3)))

#  var1 var2 var3 var4
#0    1    4    1    0
#1    1    0    1    2
#2    1    0    1    2
#3    1    0    1    0

If you don't know beforehand what levels your data can take, we can find it from data itself.

vec <- unique(unlist(df))
sapply(df, function(x) table(factor(x, levels = vec)))

Upvotes: 2

Related Questions