[R] Is this sapply behaviour normal?

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Wed Jun 25 23:14:52 CEST 2008


Rolf Turner wrote:
>
> The answer to your question is ``yeah, sort of''.  The reason for the
> difference is that mean() is generic and has a method for data frames,
> according to which the mean of each column of the data frame is found
> in some ``appropriate'' manner.  (Essentially the columns of the data
> frame must be either numeric or have some sort of date persuasion, else
> you get a warning and an NA for the column in question.  The function
> min()
> is not generic and so if you hit a data frame with min() it (apparently)
> treats that data frame as if it were an atomic vector of data and finds
> the minimum of that atomic vector.  Given, of course, that doing so makes
> sense.
>
> It would seem that you want min() to mimic the behaviour of mean().  To
> achieve this you can, in this instance at least (I think!) simply do
>
>     sapply(dats,function(x){sapply(x,min)})

you can achieve the same with

sapply(dats, sapply, min)

vQ



More information about the R-help mailing list