[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