time-data-in-R

Question:

How to convert a string with the following format ("YYYY-MM-DD HH:MM") to an object representing date and time in R?

Example

insatser$Tidpunkt[1:10]
[1] 1996-01-09 11:26 1996-01-10 18:28 1996-01-11 22:21 1996-01-13 14:40
[5] 1996-01-13 16:10 1996-01-13 20:50

Solution: (steps explained below)

dates.times.vector <- unlist(strsplit(as.vector(bränder$Tidpunkt), " "))
odd <- seq(from = 1, to = length(dates.times.vector), by = 2)
even <- seq(from = 2, to = length(dates.times.vector), by = 2)
the.dates <- dates.times.vector[odd]
the.times <- dates.times.vector[even]
the.times <- as.vector(sapply(the.times, function (x) (paste(x, ":00", sep = ""))))
the.chron.object <- chron(the.dates, the.times, format = c(dates = "y-m-d", times = "h:m:s"))
rm(dates.times.vector, odd, even, the.dates, the.times)

Split the string on " ", and store the result as a vector

dates.times.vector <- unlist(strsplit(as.vector(bränder$Tidpunkt), " "))

use seq to create an index of odds and another index of evens

odd <- seq(from = 1, to = length(dates.times.vector), by = 2)
even <- seq(from = 2, to = length(dates.times.vector), by = 2)

use odd index to get the dates, and the even index for the times.

the.dates <- dates.times.vector[odd]
the.times <- dates.times.vector[even]

add ":00" as seconds (since chron requires a value for the seconds)

the.times <- as.vector(sapply(the.times, function (x) (paste(x, ":00", sep = ""))))

create the chron object

the.chron.object <- chron(the.dates, the.times, format = c(dates = "y-m-d", times = "h:m:s"))

remove the temporary objects

rm(dates.times.vector, odd, even, the.dates, the.times)

play with your chron object

range(the.chron.object)
[1] (96-01-09 11:26:00) (96-01-22 07:24:00)

alternative without variables

the.chron.object <- chron(unlist(
                                split(
                                      unlist(
                                             strsplit(
                                                      as.vector(
                                                                insatser$Tidpunkt[1:10]
                                                               ),
                                                      " "
                                                      )
                                             ),
                                      1:2
                                      )
                                 [1],
                                 use.names = F
                                 ),
                          as.vector(
                                    sapply(
                                           split(
                                                 unlist(
                                                        strsplit(
                                                                 as.vector(
                                                                           insatser$Tidpunkt[1:10]
                                                                          ),
                                                                " "
                                                                )
                                                        ),
                                                 1:2
                                                 )
                                           [2],
                                           function(x) {paste(x, ":00", sep = "")}
                                           )
                                   ),
                         format = c(dates = "y-m-d", times = "h:m:s"))

comments powered by Disqus


Back to the index

Blog roll

R-bloggers, Debian Weekly
Valid XHTML 1.0 Strict [Valid RSS] Valid CSS! Emacs Muse Last modified: oktober 17, 2019