[R] Selecting data based on date-Data manupulation

Jim Lemon jim at bitwrit.com.au
Tue Sep 22 01:52:32 CEST 2009


On 09/21/2009 03:55 PM, premmad wrote:
> I have 70 columns and more than 400k rows .In the data date column will have
> values from 1900(01/01/1900) .How do i select only the data of recent two
> years?Help me in this regard
>    
Hi premmad,
First, find out what the two most recent dates are:

two_recent_dates<-
  sort(strptime(mydata$date,format="%d/%m/%Y")),TRUE)[1:2]

then get the row indices of those dates.

which(strptime(mydata$date,format="%d/%m/%Y") %in%
  two_recent_dates)

then select your rows:

mydata[two_recent_dates,]

Jim




More information about the R-help mailing list