[R] How to get relevant dimnames from apply() ?

Michael Dondrup Michael.Dondrup at Genetik.Uni-Bielefeld.DE
Thu Jul 11 16:31:09 CEST 2002


Jan_Svatos at eurotel.cz wrote:
Hi,
why not binding the names to the matrix? 

> 
> Hello,
> 
> in order to vectorize task (of plotting barplots), I use, as probably all
> R-folks, function apply()
> instead of for() loop.
> 
> <R>
> testarr<-matrix(1:30, nrow=5)
> rownames(testarr)<-letters[1:5]
> colnames(testarr)<-LETTERS[1:6]
> apply(testarr,1,function(x) {x11(); barplot(x)}) #the funcion used is
> actually more compex, but it doesn't matter
> </R>

This would ad something like the following to your code:

hurz <- cbind(testarr, names=rownames(testarr))
hurz
apply(hurz, 1, function(x) { 
		x11(); 
		barplot(as.numeric(x[names(x)!='nams']), 
		  names.arg = names(x[names(x)!='nams']), 
		  main=x['nams'])
		} ) 

You might decide for yourself if this is more elegant than a for loop.

Michael
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list