Re: [R] better than sapply

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat 27 Aug 2005 - 15:15:24 EST

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@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@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
>



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:34:29 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 16:00:40 EST