[R] more efficient code possible?

About this list Date view Thread view Subject view Author view Attachment view

From: Tord Snall (tord.snall@ebc.uu.se)
Date: Wed 15 Jan 2003 - 03:21:59 EST


Message-id: <3.0.6.32.20030114172159.00ad24c0@mail.anst.uu.se>

Dear all,

Does anyone have a suggestion on how to make this code more efficient?

I have a large data frame that I want to merge with another data frame

b.art.temp<- b.art[, c("BalteSFyndID", "RUBINKOD")]

I first add an indicator column for each RUBINKOD:

b.art.temp$anas.hel<- ifelse(b.art.temp$RUBINKOD=="ANAS.HEL", 1, 0)
b.art.temp$ant.pulv<- ifelse(b.art.temp$RUBINKOD=="ANT.PULV", 1, 0)
b.art.temp$anti.cur<- ifelse(b.art.temp$RUBINKOD=="ANTI.CUR", 1, 0)
...

# and then merge
b.substart<- merge(b.subst, b.art.temp[b.art.temp$anas.hel == 1,
c("BalteSFyndID", "anas.hel")], by="BalteSFyndID", all.x=T)

b.substart<- merge(b.substart, b.art.temp[b.art.temp$ant.pulv == 1,
c("BalteSFyndID", "ant.pulv")], by="BalteSFyndID", all.x=T)

b.substart<- merge(b.substart, b.art.temp[b.art.temp$anti.cur == 1,
c("BalteSFyndID", "anti.cur")], by="BalteSFyndID", all.x=T)
...

But there are many levels of RUBINKOD
nlevels(b.art.temp$RUBINKOD)
[1] 61

and I therefore wonder if there is any quicker way of doing it than
repeating the abouve lines 58 more times

Thanks in advance!

Sincerely,
Tord

-----------------------------------------------------------------------
Tord Snäll
Avd. f växtekologi, Evolutionsbiologiskt centrum, Uppsala universitet
Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University
Villavägen 14
SE-752 36 Uppsala, Sweden
Tel: 018-471 28 82 (int +46 18 471 28 82) (work)
Tel: 018-25 71 33 (int +46 18 25 71 33) (home)
Fax: 018-55 34 19 (int +46 18 55 34 19) (work)
E-mail: Tord.Snall@ebc.uu.se
Check this: http://www.vaxtbio.uu.se/resfold/snall.htm!
------------------------------------------------------------------------

______________________________________________
R-help@stat.math.ethz.ch mailing list
http://www.stat.math.ethz.ch/mailman/listinfo/r-help


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Tue 01 Jul 2003 - 09:11:19 EST