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
Summarizing each row into a frequency table
3 messages · Daren Tan, Peter Alspach, Gabor Grothendieck
Tena koe Daren One alternative: apply(m, 1, function(x) 100*summary(factor(x, levels=-1:1))/length(x[!is.na(x)])) Doubtless there are others. HTH .... Peter Alspach
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Daren Tan Sent: Tuesday, 24 March 2009 1:46 p.m. To: r-help at r-project.org Subject: [R] Summarizing each row into a frequency table 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 at 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.
The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.
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 <darentan76 at 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 at 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.