[R] "Conditional" match?

From: r user <ruser2006_at_yahoo.com>
Date: Sat 28 Jan 2006 - 07:57:07 EST


I have two datasets, big and small.

s_date<-c(‘2005-12-02’, ‘2005-12-01’,
‘2004-11-02’,’2002-10-05’,’2000-12-15’)
s_id<-c(‘a’,’a’,’b’,’c’,’d’)

b_date<- c(‘2005-12-31’, ‘2005-12-31’,
‘2004-12-31’,’2002-10-05’,’2001-10-31’,’1999-12-31’)

b_id<-c(‘a’,’b’,’c’,’d’,’e’,’c’)

small<-data.frame(date_=as.Date(s_date),id=s_id) big<-data.frame(date_=as.Date(b_date),id=b_id)

For each row in “big”, I want to look for a match in small where two conditions are met:

  1. big$id=small$id
  2. big$date_>=small$date

If match is found, I wish to return the value of the date. If no match is found, I want NA.

If more than 1 match is found, I wish to return the match where small$date is greatest.

I’m thinking I might be able to do this using the match function, and by sorting the “small” dataset by date_ in descending order.

However, I do not know how to make the match conditional on big$date_>=small$date_.

Any help is appreciated.



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 Jan 28 08:06:33 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:14 EST