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
Hello, Try the following. dat <- read.table(text=" 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 ", header=TRUE, stringsAsFactors=FALSE) dat$date <- as.Date(dat$date) str(dat) search <- as.Date("2010-10-06") max(which(dat$date <= search)) dat$y[ix] Hope this helps, Rui Barradas Em 19-07-2012 08:42, Robert Latest escreveu:> 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Hi Robert, how about this (assuming your data.frame is ordered by date): tmp<-read.table(textConnection(" 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"),header=T,colClasses=c(date="Date")) tmp[max(which(tmp$date<as.Date("2010-10-06"))),"y"] hth. Am 19.07.2012 09:42, schrieb Robert Latest:> 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Eik Vettorazzi Department of Medical Biometry and Epidemiology University Medical Center Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790 -- Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG): Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts; Gerichtsstand: Hamburg Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus
dat <- structure(list(date = structure(c(14879, 14886, 14893, 14899, 14906, 14913), class = "Date"), y = c(1356L, 1968L, 2602L, 3116L, 3496L, 3958L)), .Names = c("date", "y"), row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame") x <- as.Date( "2010-10-06" ) getY <- function( x ) { dat[ dat$date <= x, 2][ length( dat[ dat$date < x, 2] ) ] } getY( x ) [1] 1968 getY( as.Date( "2010-10-17" ) ) [1] 2602 Is that what you want? Rgds, Rainer -------- Original-Nachricht --------> Datum: Thu, 19 Jul 2012 09:42:05 +0200 > Von: Robert Latest <boblatest at gmail.com> > An: > Betreff: [R] Finding the last value before a certain date> 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- ------- Gentoo Linux with KDE