[R] selection based on dates

Farhan Ahmed farhan at farhan.org
Tue Jul 30 15:11:30 CEST 2013

  I'm still a novice at R, so this may be a bit convoluted but it works:

colnames(x) = c("date", "id", "value")
do.call(rbind, (dlply(as.data.frame(x), .(id), function (y) 
y[-c(which(as.Date(y$date, "%m/%d/%y") == min(as.Date(y$date, 
"%m/%d/%y")), arr.ind=T)),])))
- FA

------ Original Message ------
From: "Andras Farkas" <motyocska at yahoo.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Sent: 7/30/2013 8:13:21 AM
Subject: [R] selection based on dates
>Dear All
>please provide your insigths on the following:
>I have:
>a <-c("1/1/13",15,20)
>b <-c("1/5/13",15,25)
>c <-c("1/9/13",15,28)
>d <-c("2/1/13",18,30)
>e <-c("2/5/13",18,35)
>f <-c("2/9/13",18,38)
>x <-matrix(c(a,b,c,d,e,f),ncol=3,byrow=TRUE)
>What I would like to do is to eliminate certain rows of this matrix 
>based on the date column values. As you can see, in the second column 
>my values (15 and 18) repeat 3 times each, so this column serves as an 
>ID number if you will. Thus each ID numbers show up with 3 different 
>date values in the first column. Now I would like to eliminate the rows 
>with the earliest date per ID number. My result should look like this:
>z <-x[-c(1,4),]
>as allways, your help is greatly appreciated,
>R-help at r-project.org mailing list
>PLEASE do read the posting guide 
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list