[R] vectorization

Kevin Bartz bartzk at yahoo-inc.com
Fri Jun 17 20:35:54 CEST 2005


These two lines worked for me:

rst <- tapply(mydata$income, mydata$education, median)
mydata$md <- rst[mydata$education]

Here's my cheesy example:

> mydata <- data.frame(income    = round(rnorm(30000, 55000, 10000)),
+                      education = letters[rbinom(30000, 4, 1/2)+1])
> rst <- tapply(mydata$income, mydata$education, median)
> mydata$md <- rst[mydata$education]
> head(mydata)
  income education      md
1  66223         e 55094.5
2  56830         c 54966.0
3  58035         b 54937.5
4  74045         a 55213.5
5  61327         b 54937.5
6  64150         b 54937.5

Is this what you wanted?

Kevin

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Dimitri Joe
Sent: Friday, June 17, 2005 10:01 AM
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.

Thanks,

Dimitri

	[[alternative HTML version deleted]]



	
	
		
_______________________________________________________ 

Instale o discador agora! http://br.acesso.yahoo.com/

______________________________________________
R-help at 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




More information about the R-help mailing list