[R] better than sapply

2005-08-26 Thread Omar Lakkis
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,))
 r
  seid  cid 
1  NA2181   
2  NA2221   
3  NA   

 h - data.frame(seid= c(5598,5609,4931,5611,8123,8122), cid= 
 c(2219,,2181,2190,2817,2221))
 h
 cid  seid 
1 5598 2219
2 5609  
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 

__
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


Re: [R] better than sapply

2005-08-26 Thread Gabor Grothendieck
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 [EMAIL PROTECTED] 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,))
  r
  seid  cid
 1  NA2181
 2  NA2221
 3  NA
 
  h - data.frame(seid= c(5598,5609,4931,5611,8123,8122), cid= 
  c(2219,,2181,2190,2817,2221))
  h
 cid  seid
 1 5598 2219
 2 5609 
 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 
 
 __
 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