[R] better than sapply

From: Omar Lakkis <uofiowa_at_gmail.com>
Date: Sat 27 Aug 2005 - 14:51:43 EST


I have the following two mapping data frames (r) and (h). I want to fill teh value of r$seid with the value of r$seid where r$cid==h$cid. I can do it with sapply as such:

> r$seid = sapply(r$cid, function(cid) h[h$cid==cid,]$seid)

Is ther a better (faster) way to do this?

> r <- data.frame(seid=NA, cid= c(2181,2221,2222))
> r

  seid cid
1 NA 2181
2 NA 2221
3 NA 2222

> h <- data.frame(seid= c(5598,5609,4931,5611,8123,8122), cid= c(2219,2222,2181,2190,2817,2221))
> h

     cid seid
1 5598 2219
2 5609 2222
3 4931 2181
4 5611 2190
5 8123 2817
6 8122 2221   

to get the desired result of:
> r

  seid cid
1 4931 2181
2 8122 2221
3 5609 2222



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 Aug 27 15:22:57 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:56 EST