Re: [R] vectorization

From: Huntsinger, Reid <>
Date: Sat 18 Jun 2005 - 04:35:22 EST

You can use tapply() to compute the medians, as in

meds <- tapply(mydata$inc,INDEX=mydata$ed,FUN=median)

then create a new column with the medians as

medianEd <- meds[mydata$ed]

Reid Huntsinger

-----Original Message-----
[] On Behalf Of Dimitri Joe Sent: Friday, June 17, 2005 1:01 PM
To: R-Help
Subject: [R] vectorization

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.



        [[alternative HTML version deleted]]                                   

Instale o discador agora! mailing list PLEASE do read the posting guide! mailing list PLEASE do read the posting guide! Received on Sat Jun 18 04:45:44 2005

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