[R] Finding the last value before a certain date

Robert Latest boblatest at gmail.com
Thu Jul 19 09:42:05 CEST 2012


Hello all,

I have a dataframe that looks like this:

head(df)
        date    y
1 2010-09-27 1356
2 2010-10-04 1968
3 2010-10-11 2602
4 2010-10-17 3116
5 2010-10-24 3496
6 2010-10-31 3958

I need a function that, given any date, returns the y value
corresponding to the given date or the last day before the given date.

Example:

Input: as.Date("2010-10-06"). Output: 1968 (because the last value is
from 2010-10-04)

I've been tinkering with this for an hour now, without success. I
think the solution is either surprisingly complicated or surprisingly
simple.

Thanks,
robert



More information about the R-help mailing list