1. This is probably overkill, but works:
> as.data.frame(table(as.data.frame(m)))
V1 V2 V3 Freq
1 0 0 0 0
2 1 0 0 2
3 0 1 0 3
4 1 1 0 0
5 0 0 1 1
6 1 0 1 0
7 0 1 1 0
8 1 1 1 0
You can easily get rid of 0-frequency rows afterward.
2. Not sure what you want, but guessing something like:
m.sorted <- m[order(rowSums(m)), order(colSums(m))]
Andy
________________________________
From: r-help-bounces@stat.math.ethz.ch on behalf of Stefan Nachtnebel
Sent: Sat 3/24/2007 8:41 AM
To: r-help@stat.math.ethz.ch
Subject: [R] frequency tables and sorting by rowSum [Broadcast]
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
______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments,...{{dropped}}