+ Reply to Thread
Results 1 to 7 of 7

Thread: Timestamp editing help

  1. #1
    Points: 836, Level: 15
    Level completed: 36%, Points required for next Level: 64

    Posts
    14
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Timestamp editing help



    Hi,

    I have a column of data in my data set which is a datestamp, looking like this:

    2012-05-29 06:56:18

    This is in one column, but I need to sort and analyze the data based on the time of day, and not the day of the year.

    Is there a simple way to separate these values into two different columns?

  2. #2
    TS Contributor
    Points: 6,706, Level: 53
    Level completed: 78%, Points required for next Level: 44
    Lazar's Avatar
    Location
    Sydney
    Posts
    676
    Thanks
    111
    Thanked 168 Times in 153 Posts

    Re: Timestamp editing help

    This is really clunky but it works:
    Code: 
    #Fake data frame
    data <- as.data.frame(rep("2012-05-29 06:56:18", 100))
    #Making a new column in data frame with just time
    data[,2]<- unlist(strsplit(as.character(data[,1]), " "))[c(F,T)]
    This is ugly code and I am sure someone with knowledge of manipulating dates in R will give a much better answer.

  3. #3
    Cookie Scientist
    Points: 6,080, Level: 50
    Level completed: 65%, Points required for next Level: 70
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    814
    Thanks
    18
    Thanked 320 Times in 245 Posts

    Re: Timestamp editing help

    Check out the 'lubridate' package.
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

  4. #4
    TS Contributor
    Points: 6,706, Level: 53
    Level completed: 78%, Points required for next Level: 44
    Lazar's Avatar
    Location
    Sydney
    Posts
    676
    Thanks
    111
    Thanked 168 Times in 153 Posts

    Re: Timestamp editing help

    Jake the lubricate package seems to have functions for extracting hour(x),minute(x),second(x) but nothing for time. Is this right? The closest I could get was

    Code: 
    library(lubridate)
    x <- ymd_hms(rep("2012-05-29 06:56:18", 100))
    paste(hour(x),minute(x),second(x), sep=":")

  5. #5
    Cookie Scientist
    Points: 6,080, Level: 50
    Level completed: 65%, Points required for next Level: 70
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    814
    Thanks
    18
    Thanked 320 Times in 245 Posts

    Re: Timestamp editing help

    The goal is to sort the dates by time of day, right? How about this:
    Code: 
    > x <- ymd_hms(rep("2012-05-29 06:56:18", 10))
    Using date format %Y-%m-%d.
    > hour(x) <- round(runif(10, 0, 23))
    > minute(x) <- round(runif(10, 0, 59))
    > second(x) <- round(runif(10, 0, 59))
    > # in random order
    > x
     [1] "2012-05-29 19:09:20 UTC" "2012-05-29 07:00:44 UTC"
     [3] "2012-05-29 16:22:34 UTC" "2012-05-29 03:59:54 UTC"
     [5] "2012-05-29 06:19:14 UTC" "2012-05-29 13:14:15 UTC"
     [7] "2012-05-29 05:27:44 UTC" "2012-05-29 06:25:33 UTC"
     [9] "2012-05-29 21:49:07 UTC" "2012-05-29 22:24:14 UTC"
    > 
    > # sort by time of day
    > x[order(second(x) + 60*minute(x) + 60*60*hour(x))]
     [1] "2012-05-29 03:59:54 UTC" "2012-05-29 05:27:44 UTC"
     [3] "2012-05-29 06:19:14 UTC" "2012-05-29 06:25:33 UTC"
     [5] "2012-05-29 07:00:44 UTC" "2012-05-29 13:14:15 UTC"
     [7] "2012-05-29 16:22:34 UTC" "2012-05-29 19:09:20 UTC"
     [9] "2012-05-29 21:49:07 UTC" "2012-05-29 22:24:14 UTC"
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

  6. #6
    TS Contributor
    Points: 6,706, Level: 53
    Level completed: 78%, Points required for next Level: 44
    Lazar's Avatar
    Location
    Sydney
    Posts
    676
    Thanks
    111
    Thanked 168 Times in 153 Posts

    Re: Timestamp editing help

    That works or even more simply:
    Code: 
     x[order(as.numeric(x - as.Date(x)))]

  7. The Following 2 Users Say Thank You to Lazar For This Useful Post:

    Dason (08-02-2012), trinker (08-02-2012)

  8. #7
    Cookie Scientist
    Points: 6,080, Level: 50
    Level completed: 65%, Points required for next Level: 70
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    814
    Thanks
    18
    Thanked 320 Times in 245 Posts

    Re: Timestamp editing help


    Clever approach. Although it's a little annoying that, at least for me, it throws a warning message :\
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts








Advertise on Talk Stats