[R] more efficient code possible?

Tord Snall tord.snall at ebc.uu.se
Tue Jan 14 17:39:46 CET 2003


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 at ebc.uu.se
Check this: http://www.vaxtbio.uu.se/resfold/snall.htm!
------------------------------------------------------------------------




More information about the R-help mailing list