Re: [R] apply vs. sapply

About this list Date view Thread view Subject view Author view Attachment view

From: Uwe Ligges (ligges@statistik.uni-dortmund.de)
Date: Sun 22 Dec 2002 - 04:29:32 EST


Message-id: <3E04A4FC.48245EA7@statistik.uni-dortmund.de>

Christian Schulz wrote:
>
> Hi,
>
> sapply((1:NCOL(hermes)),function(x) hist(hermes[,x],main=names(hermes)[x]))
>
> .......this works , but i would like use it with apply to generate many plots in one step!

sapply() already *has* generated many plots, if NCOL(hermes) > 1.
It's rather complicated to help if one does not know what kind of object
"hermes" is [I guess a data.frame, because names() seems to give
reasonable results for you]

> ####################################################################
>
> >>apply((1:ncol(hermes)),2,function(x) hist(hermes[,x],main=names(hermes)[x]))
> Error in apply(1:ncol(hermes), 2, function(x) hist(hermes[, x], main = names(hermes)[x])) :
> dim(X) must have a positive length

apply() expects an array or a matrix, but not a vector.
The function within apply works on rows / columns of the matrix given as
the first argument, so something like
 apply(hermes, 2, hist)
should do the trick.

Anyway, for labeling purposes I'd highly recommend to use a loop rather
than apply().

Uwe Ligges

 
> >>apply(hermes[,1:6],2,function(x) hist(hermes[,x],main=names(hermes)[x]))
> Error in hist.default(hermes[, x], main = names(hermes)[x]) :
> `x' must be numeric
>
> >>apply(hermes,2,function(x) hist(hermes[,x],main=names(hermes)[x]))
> Error in hist.default(hermes[,x ], main = names(hermes)[x]) :
> `x' must be numeric
>
> thanks for advance & regards
>
> [[alternate HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help

______________________________________________
R-help@stat.math.ethz.ch mailing list
http://www.stat.math.ethz.ch/mailman/listinfo/r-help


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Tue 01 Jun 2004 - 09:31:59 EST