[R] frequency tables and sorting by rowSum

Stefan Nachtnebel stefan.na at gmx.net
Sat Mar 24 13:41:15 CET 2007


Dear list, 

I have some trouble generating a frequency table over a number of vectors. 
Creating these tables over simple numbers is no problem with table()

> table(c(1,1,1,3,4,5))

1 3 4 5
3 1 1 1


, but how can i for example turn:
0 1 0
0 0 1
0 1 0
1 0 0
0 1 0
1 0 0

into

0 0 1 1
1 0 0 2
0 1 0 3

My second problem is, sorting rows and columns of a matrix by the rowSums/colSums.
I did it this way, but i think there should be a more efficient way:

sortRowCol<-function(taus) {
  swaprow <- function(rsum) {
    taus[(rowSums(taus)==rsum),]
  }
  for( i in 1:2 )
     taus<-sapply(sort(rowSums(taus)),swaprow)
}

thanks in advantage, Stefan Nachtnebel

-- 
"Feel free" - 5 GB Mailbox, 50 FreeSMS/Monat ...
Jetzt GMX ProMail testen: www.gmx.net/de/go/mailfooter/promail-out



More information about the R-help mailing list