[R] Summarizing each row into a frequency table
William Dunlap
wdunlap at tibco.com
Tue Mar 24 19:58:45 CET 2009
Have you tried
table(row(m), m)
? If there is a chance that m doesn't have
at least 1 of each of c(-1,0,1) then force them
into the table with
table(row(m), factor(m, levels=c(-1,0,1)))
Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com
------------------------------------------------------------
[R] Summarizing each row into a frequency table
Daren Tan darentan76 at gmail.com
Tue Mar 24 01:46:21 CET 2009
I have a matrix containing -1, 0, 1, however certain rows will not
have all 3 numbers. I have written some codes to compute the frequency
table of how many -1s, 0s, 1s per row, but it is very ugly and not
efficient if there are more than 3 numbers. Please suggest.
m <- rbind(sample(0:1, replace=T, 10), sample(-1:1, replace=T, 10))
m.table <- t(apply(m, 1, function(x) c(sum(x==-1, na.rm=T), sum(x==0,
na.rm=T), sum(x==1, na.rm=T)) ))
m.table <- prop.table(m.table, 1)*100
colnames(m.table) <- -1:1
More information about the R-help
mailing list