[R] Joining two (or more) frequency tables

Wu Gong ghowoo at gmail.com
Wed May 19 16:36:15 CEST 2010


> ## Create a sample data.
> data <- data.frame(father.id = letters[1:5],
+ diagnosis = sample(c(100,200,300,340),5,replace=TRUE),
+ diagnosis1 = sample(c(100,200,300,340),5,replace=TRUE),
+ diagnosis2 = sample(c(100,200,300,340),5,replace=TRUE))
> data
  father.id diagnosis diagnosis1 diagnosis2
1         a       340        100        300
2         b       200        300        340
3         c       200        300        100
4         d       200        100        300
5         e       200        100        100
> 
> ## Create a matrix by replicating the father.id column 3 times
> ## Because we have 3 diagnosis columns to match when use table() function
> match <- as.matrix(data[,rep(1:2,c(3,0))])
> match
     father.id father.id.1 father.id.2
[1,] "a"       "a"         "a"        
[2,] "b"       "b"         "b"        
[3,] "c"       "c"         "c"        
[4,] "d"       "d"         "d"        
[5,] "e"       "e"         "e"        
> 
> ## Count
> table(match,as.matrix(data[,2:4]))
     
match 100 200 300 340
    a   1   0   1   1
    b   0   1   1   1
    c   1   1   1   0
    d   1   1   1   0
    e   2   1   0   0
> 


Please refer to:
http://r.789695.n4.nabble.com/Counting-Frequencies-in-Data-Frame-tt2221342.html#a2221487
http://r.789695.n4.nabble.com/Counting-Frequencies-in-Data-Frame-tt2221342.html#a2221487 

-----
A R learner.
-- 
View this message in context: http://r.789695.n4.nabble.com/Joining-two-or-more-frequency-tables-tp2222576p2222946.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list