[R] error in using by + median

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri May 2 16:10:40 CEST 2008


The help says

      A data frame is split by row into data frames subsetted by the
      values of one or more factors, and function 'FUN' is applied to
      each subset in turn.

You attempting to apply median() to a data frame -- it does no work, 
unlike mean()

On Fri, 2 May 2008, vito muggeo wrote:

> dear all,
> Could anyone explain me the behaviour of median() within by()?
> (I am running R.2.7.0)
>
> thanks,
> vito
>
>> H<-cbind(rep(0:1,l=20),matrix(rnorm(20*2),20,2))
>> by(H[,-1],H[,1],mean)
> INDICES: 0
>        V1         V2
> -0.2101069  0.2954377
> --------------------------------------------------------------------------------------------------------------------- 
> INDICES: 1
>         V1          V2
> -0.23682173 -0.01225147
>> by(H[,-1],H[,1],median)
> Error in median.default(data[x, , drop = FALSE], ...) : need numeric data
>>
>
>
> -- 
> ====================================
> Vito M.R. Muggeo
> Dip.to Sc Statist e Matem `Vianelli'
> Università di Palermo
> viale delle Scienze, edificio 13
> 90128 Palermo - ITALY
> tel: 091 6626240
> fax: 091 485726/485612
> http://dssm.unipa.it/vmuggeo
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list