[R] better than sapply

Gabor Grothendieck ggrothendieck at gmail.com
Sat Aug 27 07:15:24 CEST 2005


I don't know if its faster but you could try timing this to find out:

r$seid <- merge(h, r, by = "cid")[,2]


On 8/27/05, Omar Lakkis <uofiowa at gmail.com> wrote:
> 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 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