[R] better than sapply

Omar Lakkis uofiowa at gmail.com
Sat Aug 27 06:51:43 CEST 2005


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




More information about the R-help mailing list