Jan_Svatos@eurotel.cz
2002-Jul-11 08:18 UTC
[R] How to get relevant dimnames from apply() ?
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> Now, how I can propagate the rownames of testarr into the main label of plot via main="what_should_I_paste_here?" Asking for <R> str(apply(testarr,1,function(x) {x11(); barplot(x)})) </R> gives <Routput> num [1:6, 1:5] 0.7 1.9 3.1 4.3 5.5 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:5] "a" "b" "c" "d" ... </Routput> but how it is possible to put dimnames of _this_ (i.e., the rownames of testarr) into "main" label of a plot? With for() loop, the task is trivial, but with apply? The problem I need to solve is OK (in terms of performance) with for() loops, but apply is more elegant and , of course, much more in R-style... Thanks, Jan ------------------------------------------------- designed for _monospaced_ font ------------------------------------------------- /- Jan Svatos, PhD Sokolovska 855/225 -/ /- Data Analyst Prague 9 -/ /- Eurotel Praha 190 00 -/ /- jan_svatos at eurotel.cz Czechia -/ ------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._