How to convert a string with the following format ("YYYY-MM-DD HH:MM") to an object representing date and time in R?
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
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)
dates.times.vector <- unlist(strsplit(as.vector(bränder$Tidpunkt), " "))
seq to create an index of odds and another index of evensodd <- 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 = ""))))
chron objectthe.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)
range(the.chron.object) [1] (96-01-09 11:26:00) (96-01-22 07:24:00)
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"))