[R] Convert the column values to frequencies

arun smartpink111 at yahoo.com
Mon Jun 9 13:59:43 CEST 2014


Hi,

Please use ?dput() to show the data.
Assuming that the data is:
dat <- structure(list(X = c("ks", "ks", "tk", "tk", "tk", "ks", "eq", 
"eq", "ks"), Y = c("dr", "zw", "dr", "", "zw", "zw", "", "zw", 
"zw"), Z = c("tq", "tq", "tq", "", "tz", "", "tz", "tz", "tz"
)), .Names = c("X", "Y", "Z"), class = "data.frame", row.names = c(NA, 
-9L))

dat[] <- lapply(dat, function(x) table(x)[x])
dat
A.K.



Hi,
I try to convert the column values to the number of the different column values (factor levels) - so as the frequency of each column value. Here my_table data including missing values:

                            X         Y       Z
                           ks         dr      tq
                           ks         zw      tq
                           tk         dr      tq
                           tk    
                           tk        zw      tz
                           ks        zw    
                           eq            tz
                           eq       zw         tz  
                           ks       zw        tz


So I want:
                          X   Y          Z
                          4   2          3
                          4       5       3
                          3       2       3
                          3  
                          3       5       4
                          4       5  
                          2       4
                          2       5       4
                          4       5       4

I tried lapply(my_table, table). But that is not what I want to have. How can I solve this problem? 




More information about the R-help mailing list