Try this (haven't checked the speed): f <- function(x) table(factor(x, c(-1, 0, 1))) 100 * prop.table(t(apply(m, 1, f)), 1)
On Mon, Mar 23, 2009 at 8:46 PM, Daren Tan <darenta...@gmail.com> wrote: > 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 > > ______________________________________________ > R-help@r-project.org 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. > ______________________________________________ R-help@r-project.org 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.