r - create a matrix from data frame -


i have data frame categorical values

names   dis   del       0-2   0-2       2-4   0-2       6-8   6-8     b  8-10  8-10     c   10+   10+ 

what want output in number of count per data

       0-2  2-4  6-8  8-10  10+        0-2     1                         2-4     1                      6-8               1            8-10                     1     10+                          1   

i want export data created out of data frame.

from comments of @mtoto & @jogo:

table(mydf[-1]) 

or:

xtabs(data=mydf, ~ dis+del) 

both give:

      del dis    0-2 10+ 6-8 8-10   0-2    1   0   0    0   10+    0   1   0    0   2-4    1   0   0    0   6-8    0   0   1    0   8-10   0   0   0    1 

if want levels in correct order (10+ last one):

mydf$dis <- factor(mydf$dis, levels = c("0-2","2-4","6-8","8-10","10+")) mydf$del <- factor(mydf$del, levels = c("0-2","6-8","8-10","10+")) 

now get:

      del dis    0-2 6-8 8-10 10+   0-2    1   0    0   0   2-4    1   0    0   0   6-8    0   1    0   0   8-10   0   0    1   0   10+    0   0    0   1 

used data:

mydf <- read.table(text="names   dis   del       0-2   0-2       2-4   0-2       6-8   6-8     b  8-10  8-10     c   10+   10+", header=true) 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -