[R] How to filter datetime from a dataframe

Anders Ellern Bilgrau anders.ellern.bilgrau at gmail.com
Wed Jul 25 10:10:21 CEST 2012


Hi,

Check out

?as.Date

which is the key function for what you want. Objects of the class "Date" can be compared using logical operators. Try running the following code. As you didn't provide any data to illustrate your problem I've created some mockup data.

data <- data.frame(cbind(date = c("01/Apr/2012", "03/Jul/2012", "12/Jul/2012", "09/Jul/2012"), surv = c(4, 3, 5, 2)))
data

data$date <- as.Date(data$date, format = "%d/%b/%Y")
data$date
get.rows   <- data$date >= as.Date("2012-07-01") & data$date <= as.Date("2012-07-10")
data <- data[get.rows, ]
data

I hope this is approximately what you want.

Regards, Anders B


On 25/07/2012, at 09.05, purushothaman wrote:

> I have one month data in a dataframe..
> 
> I need to filter data based on start date and end date 
> Ex: Start date :- 01/Jul/2012 and End date :- 10/Jul/2012 
> 
> Please help me to find the solution
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-filter-datetime-from-a-dataframe-tp4637721.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list