How to filter datetime from a dataframe

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to filter datetime from a dataframe

purushothaman
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
Reply | Threaded
Open this post in threaded view
|

Re: How to filter datetime from a dataframe

Anders E Bilgrau
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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to filter datetime from a dataframe

Rui Barradas
In reply to this post by purushothaman
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:

> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to filter datetime from a dataframe

purushothaman
In reply to this post by purushothaman
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
Reply | Threaded
Open this post in threaded view
|

Re: How to filter datetime from a dataframe

Rui Barradas
Hello,

inline
Em 25-07-2012 11:08, 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".

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

>
> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to filter datetime from a dataframe

arun kirshna
In reply to this post by purushothaman
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 <[hidden email]>
To: [hidden email]
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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to filter datetime from a dataframe

Jeff Newmiller
In reply to this post by purushothaman
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:<[hidden email]>        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 <[hidden email]> wrote:

>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.
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.