[R] problem with unlist

Liaw, Andy andy_liaw at merck.com
Mon Feb 23 17:03:14 CET 2004


Why not just do:

tapply(y, list(g1, g2, ...), function(x) c(mean=mean(x, na.rm=TRUE),
sd=sd(x, na.rm=TRUE), n=sum(!is.na(x))))

??

Andy

> From: r.ghezzo
> 
> Hello, I try to compute means,sd,n from a variable y 
> according to some 
> categoricals g1,g2... I wrote the following function:
> msd<-function(y,...) {
>     print(match.call())
>     funy<-function(x) 
> list(mean(x,na.rm=T),sd(x,na.rm=T),table(is.na(x))[1])
>     gg <- list(...)
>     aa<-by(y,gg,funy)
>     bb<-matrix(unlist(aa),nrow=3)
>     bb
> }
> this works OK so far there is no empty cell in the crosstab, if there 
> is, the unlist statement just run over it and I do not know 
> which cell 
> is missing, any attempt to label the columns in matrix bb then fail.
> Can somebody suggest a method to retrive the list names from 
> aa to label 
> the columns of the matrix bb?
> Thanks for any help
> Heberto Ghezzo
> McGill University
> Montreal Canada
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}




More information about the R-help mailing list