Skip to content

How to filter datetime from a dataframe

7 messages · Anders Ellern Bilgrau, purushothaman, Rui Barradas +2 more

#
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.
#
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:

            
#
Hello,

It's not the first time you post questions to R-Help, so
1. Where is the reproducible example?
2. The form you post [part of] your data example forces us to arrange it 
in order to become R code.

Try the following.

StartDate <- as.Date("01/Jul/2012", format="%d/%B/%Y")
EndDate <- as.Date("10/Jul/2012", format="%d/%B/%Y")

set.seed(8356)
x <- as.Date("2012-06-01") + cumsum(sample(4, 20, TRUE))

inx <- StartDate < x & x < EndDate
x[inx]

Hope this helps,

Rui Barradas

Em 25-07-2012 08:05, purushothaman escreveu:
#
Thanks for your reply..

The solution what you have provided, working fine as per if the dataframe
has only the "date" in the "date-column", But, in my scenario, i have 
"date" and "time" in the "date-column".

So could you please help me to filter the "date" and "time" from the
dataframe.

Thanks in advance,
Purushothaman 



--
View this message in context: http://r.789695.n4.nabble.com/How-to-filter-datetime-from-a-dataframe-tp4637721p4637755.html
Sent from the R help mailing list archive at Nabble.com.
#
Hello,

inline
Em 25-07-2012 11:08, purushothaman escreveu:
But that is not the data example you have given. Anyway it's almost the 
same. Just change as.Date to as.POSIXct and change the format to 
something like "%d/%B/%Y %H:%M:%S". Then apply the comparison operators 
as already seen.

Also, you should provide a data example, specially after saying that 
your scenario is not as you had previously described. And quote context, 
in Nabble there's a "quote" button, use it.

Hope this helps,

Rui Barradas
#
Hi,
Try this:

date1<-c("26/Jun/2012","27/Jun/2012","28/Jun/2012","29/Jun/2012","01/Jul/2012","02/Jul/2012","03/Jul/2012","04/Jul/2012", "15/Jul/2012","20/Jul/2012","05/Jul/2012","06/Jul/2012","07/May/2012","08/Jul/2012","09/Jul/2012")
date2<-as.Date(date1,format="%d/%b/%Y")
dat2<-data.frame(var1=sample(1:15,15,replace=T),date2)
subset(dat2,dat2$date2>"2012-07-01"&dat2$date2<"2012-07-10")
?? var1????? date2
6???? 3 2012-07-02
7??? 10 2012-07-03
8??? 14 2012-07-04
11?? 10 2012-07-05
12?? 14 2012-07-06
14?? 14 2012-07-08
15?? 11 2012-07-09

A.K.



----- Original Message -----
From: purushothaman <purushothaman.b at ge.com>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, July 25, 2012 3:05 AM
Subject: [R] How to filter datetime from a dataframe

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.
#
Please read the posting guide mentioned at the bottom of any message on this list. Your posts have issues:

Reproducible example (use dput)
desired output (use dput)
Include context from previous messages
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
purushothaman <purushothaman.b at ge.com> wrote: