Re: [R] vectorization

From: Liaw, Andy <andy_liaw_at_merck.com>
Date: Sat 18 Jun 2005 - 04:21:18 EST


Here I go again with ave():

mydata$md <- ave(mydata$income, mydata$education, FUN=median, na.rm=TRUE)

IMHO it's one of the most under-rated helper functions in R.

Andy

> From: Dimitri Joe
>
> Hi there,
>
> I have a data frame (mydata) with 1 numeric variable (income)
> and 1 factor (education). I want a new column in this data
> with the median income for each education level. A obviously
> inneficient way to do this is
>
> for ( k in 1: nrow(mydata) ) {
> l <- mydata$education[k]
> mydata$md[k] <- median(mydata$income[mydata$education==l],na.rm=T)
> }
>
> Since mydata has nearly 30.000 rows, this will be done not
> untill the end of this month. I thus need some help for
> vectorizing this, please.
>
> Thanks,
>
> Dimitri
>
> [[alternative HTML version deleted]]
>
>
>
>
>
>
> _______________________________________________________
>
> Instale o discador agora! http://br.acesso.yahoo.com/
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat Jun 18 04:38:52 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:48 EST