Skip to content

apply vs. sapply

3 messages · Uwe Ligges, Christian Schulz

#
Christian Schulz wrote:
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() 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
#
....hmm sorry "sometimes after several experiments" my graphic.device
(W2000/1.6.1)didn't popUp & only
a new start helps, but now it works :-)

# hermes is data.frame
apply(hermes,2,hist) #without names
sapply((1:ncol(hermes)),function(x) hist(hermes[,x],main=names(hermes)[x]))
#with col.names

thanks & regards,christian