[R] Summarizing each row into a frequency table

Peter Alspach PAlspach at hortresearch.co.nz
Tue Mar 24 02:11:42 CET 2009


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.




More information about the R-help mailing list