Converting a dataframe column from string to datetime

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Converting a dataframe column from string to datetime

rajeshj@cse.iitm.ac.in

Hi,

I have a dataframe column of the form
v<-c("Fri Feb 05 20:00:01.43000 2010","Fri Feb 05 20:00:02.274000 2010","Fri Feb 05 20:00:02.274000 2010","Fri Feb 05 20:00:06.34000 2010")

I need to convert this to datetime form. I did the following..

lapply(v,function(x){strptime(x, "%a %b %d %H:%M:%OS %Y")})

This gives me a list that looks like this...

[[1]]
[1] "2010-02-05 20:00:01.43"
[[2]]
[1] "2010-02-05 20:00:02.274"
[[3]]
[1] "2010-02-05 20:00:02.274"
[[4]]
[1] "2010-02-05 20:00:06.34"

However, when I do an unlist...I gets converted to something like this...

sec min    hour    mday mon    year    wday    yday   isdst sec min    hour    mday mon    year    wday    yday   isdst sec
  1.430   0.000  20.000   5.000   1.000 110.000   5.000  35.000   0.000   2.274   0.000  20.000   5.000   1.000 110.000   5.000  35.000   0.000   2.274
    min    hour    mday mon    year    wday    yday   isdst sec min    hour    mday mon    year    wday    yday   isdst
  0.000  20.000   5.000   1.000 110.000   5.000  35.000   0.000   6.340   0.000  20.000   5.000   1.000 110.000   5.000  

I want it to become a dataframe column except for a change in the datatype to datetime...how can I achieve this?
        [[alternative HTML version deleted]]

______________________________________________
[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: Converting a dataframe column from string to datetime

Arun.stat
Is it okay with you?

Reduce("rbind", lapply(lapply(v,function(x){strptime(x, "%a %b %d %H:%M:%OS %Y")}), as.character))
Reply | Threaded
Open this post in threaded view
|

Re: Converting a dataframe column from string to datetime

jholtman
In reply to this post by rajeshj@cse.iitm.ac.in
I think you want to use as.POSIXct to get the date value:

> v<-c("Fri Feb 05 20:00:01.43000 2010","Fri Feb 05 20:00:02.274000 2010","Fri Feb 05 20:00:02.274000 2010","Fri Feb 05 20:00:06.34000 2010")
> x <- as.POSIXct(v, format= "%a %b %d %H:%M:%OS %Y")
> x
[1] "2010-02-05 20:00:01 EST" "2010-02-05 20:00:02 EST"
[3] "2010-02-05 20:00:02 EST" "2010-02-05 20:00:06 EST"
> str(x)
 POSIXct[1:4], format: "2010-02-05 20:00:01" "2010-02-05 20:00:02" ...


On Fri, Oct 1, 2010 at 1:59 AM, [hidden email]
<[hidden email]> wrote:

>
> Hi,
>
> I have a dataframe column of the form
> v<-c("Fri Feb 05 20:00:01.43000 2010","Fri Feb 05 20:00:02.274000 2010","Fri Feb 05 20:00:02.274000 2010","Fri Feb 05 20:00:06.34000 2010")
>
> I need to convert this to datetime form. I did the following..
>
> lapply(v,function(x){strptime(x, "%a %b %d %H:%M:%OS %Y")})
>
> This gives me a list that looks like this...
>
> [[1]]
> [1] "2010-02-05 20:00:01.43"
> [[2]]
> [1] "2010-02-05 20:00:02.274"
> [[3]]
> [1] "2010-02-05 20:00:02.274"
> [[4]]
> [1] "2010-02-05 20:00:06.34"
>
> However, when I do an unlist...I gets converted to something like this...
>
> sec      min    hour    mday     mon    year    wday    yday   isdst     sec     min    hour    mday     mon    year    wday    yday   isdst     sec
>  1.430   0.000  20.000   5.000   1.000 110.000   5.000  35.000   0.000   2.274   0.000  20.000   5.000   1.000 110.000   5.000  35.000   0.000   2.274
>    min    hour    mday  mon    year    wday    yday   isdst     sec     min    hour    mday     mon    year    wday    yday   isdst
>  0.000  20.000   5.000   1.000 110.000   5.000  35.000   0.000   6.340   0.000  20.000   5.000   1.000 110.000   5.000
>
> I want it to become a dataframe column except for a change in the datatype to datetime...how can I achieve this?
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
[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: Converting a dataframe column from string to datetime

MacQueen, Don
In reply to this post by rajeshj@cse.iitm.ac.in
You’re working too hard. Use this:

   tms <- as.POSIXct(strptime(v, "%a %b %d %H:%M:%OS %Y"))

Take note of the fact that there are two types of datetime objects:  POSIXct and POSIXlt.

Your unlist() gave what seemed a strange result because you used on an “lt” object. Had you given it a “ct” object it would have made sense. To see, try
   lapply(v,function(x){as.POSIXct(strptime(x, "%a %b %d %H:%M:%OS %Y"))})

But using lapply() was more complicated than necessary.

-Don

On 9/30/10 10:59 PM, "[hidden email]" <[hidden email]> wrote:


Hi,

I have a dataframe column of the form
v<-c("Fri Feb 05 20:00:01.43000 2010","Fri Feb 05 20:00:02.274000 2010","Fri Feb 05 20:00:02.274000 2010","Fri Feb 05 20:00:06.34000 2010")

I need to convert this to datetime form. I did the following..

lapply(v,function(x){strptime(x, "%a %b %d %H:%M:%OS %Y")})

This gives me a list that looks like this...

[[1]]
[1] "2010-02-05 20:00:01.43"
[[2]]
[1] "2010-02-05 20:00:02.274"
[[3]]
[1] "2010-02-05 20:00:02.274"
[[4]]
[1] "2010-02-05 20:00:06.34"

However, when I do an unlist...I gets converted to something like this...

sec  min    hour    mday  mon    year    wday    yday   isdst  sec  min    hour    mday  mon    year    wday    yday   isdst  sec
  1.430   0.000  20.000   5.000   1.000 110.000   5.000  35.000   0.000   2.274   0.000  20.000   5.000   1.000 110.000   5.000  35.000   0.000   2.274
    min    hour    mday  mon    year    wday    yday   isdst  sec  min    hour    mday  mon    year    wday    yday   isdst
  0.000  20.000   5.000   1.000 110.000   5.000  35.000   0.000   6.340   0.000  20.000   5.000   1.000 110.000   5.000

I want it to become a dataframe column except for a change in the datatype to datetime...how can I achieve this?
 [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://BLOCKEDstat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://BLOCKEDwww.BLOCKEDR-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


--
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
925 423-1062

        [[alternative HTML version deleted]]


______________________________________________
[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: Converting a dataframe column from string to datetime

rajeshj@cse.iitm.ac.in
ok..thanks

----- Original Message -----
From: Don MacQueen <[hidden email]>
To: [hidden email], r-help <[hidden email]>
Sent: Fri, 01 Oct 2010 20:51:06 +0530 (IST)
Subject: Re: [R] Converting a dataframe column from string to datetime

You’re working too hard. Use this:



   tms <- as.POSIXct(strptime(v, "%a %b %d %H:%M:%OS %Y"))



Take note of the fact that there are two types of datetime objects:  POSIXct and POSIXlt.



Your unlist() gave what seemed a strange result because you used on an “lt” object. Had you given it a “ct” object it would have made sense. To see, try


   lapply(v,function(x){as.POSIXct(strptime(x, "%a %b %d %H:%M:%OS %Y"))})



But using lapply() was more complicated than necessary.



-Don



On 9/30/10 10:59 PM, "[hidden email]" <[hidden email]> wrote:





Hi,



I have a dataframe column of the form


v<-c("Fri Feb 05 20:00:01.43000 2010","Fri Feb 05 20:00:02.274000 2010","Fri Feb 05 20:00:02.274000 2010","Fri Feb 05 20:00:06.34000 2010")



I need to convert this to datetime form. I did the following..



lapply(v,function(x){strptime(x, "%a %b %d %H:%M:%OS %Y")})



This gives me a list that looks like this...



[[1]]


[1] "2010-02-05 20:00:01.43"


[[2]]


[1] "2010-02-05 20:00:02.274"


[[3]]


[1] "2010-02-05 20:00:02.274"


[[4]]


[1] "2010-02-05 20:00:06.34"



However, when I do an unlist...I gets converted to something like this...



sec  min    hour    mday  mon    year    wday    yday   isdst  sec  min    hour    mday  mon    year    wday    yday   isdst  sec


  1.430   0.000  20.000   5.000   1.000 110.000   5.000  35.000   0.000   2.274   0.000  20.000   5.000   1.000 110.000   5.000  35.000   0.000   2.274


        min    hour    mday  mon    year    wday    yday   isdst  sec  min    hour    mday  mon    year    wday    yday   isdst  


  0.000  20.000   5.000   1.000 110.000   5.000  35.000   0.000   6.340   0.000  20.000   5.000   1.000 110.000   5.000  



I want it to become a dataframe column except for a change in the datatype to datetime...how can I achieve this?


 [[alternative HTML version deleted]]



______________________________________________
[hidden email] mailing list
https://BLOCKEDstat.ethz.ch/mailman/listinfo/r-help


PLEASE do read the posting guide http://BLOCKEDwww.BLOCKEDR-project.org/posting-guide.html


and provide commented, minimal, self-contained, reproducible code.


--


Don MacQueen


Environmental Protection Department


Lawrence Livermore National Laboratory


925 423-1062





        [[alternative HTML version deleted]]


______________________________________________
[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: Converting a dataframe column from string to datetime

arun kirshna
This post has NOT been accepted by the mailing list yet.
This post was updated on .
In reply to this post by MacQueen, Don
Hi,
  a<-lapply(v,function(x){as.POSIXct(strptime(x, "%a %b %d %H:%M:%OS %Y"))})
do.call("c",a)
[1] "2010-02-05 20:00:01 EST" "2010-02-05 20:00:02 EST"
[3] "2010-02-05 20:00:02 EST" "2010-02-05 20:00:06 EST"
  b<-lapply(v,function(x){as.POSIXlt(strptime(x, "%a %b %d %H:%M:%OS %Y"))})
 do.call("c",b)
[1] "2010-02-05 20:00:01 EST" "2010-02-05 20:00:02 EST"
[3] "2010-02-05 20:00:02 EST" "2010-02-05 20:00:06 EST"

a1<-do.call("c",a)
b1<-do.call("c",b)
 str(a1)
 POSIXct[1:4], format: "2010-02-05 20:00:01" "2010-02-05 20:00:02" ...
 str(b1)
 POSIXlt[1:4], format: "2010-02-05 20:00:01" "2010-02-05 20:00:02" ...
 b1<-as.POSIXct(a1)
 identical(a1,b1)
[1] TRUE

A.K.