Reputation: 61
I have created a file something like this:
> filesInside
Date Time
1 01:09:2013 10:35:49.997
2 01:09:2013 10:35:50.197
How could I possibly make a function using
as.POSIXct()
and I should be get something like this:
> as.POSIXct("2013-09-01 10:35:50")
[1] "2013-09-01 10:35:50 NZST"
How can I make it as a function?
My code so far:
DateTime <- as.POSIXct(paste(filesInside$Date, filesInside$Time), format="%Y%m%d %H%M%S")
Appreciate a bit of help please. Cheers
Upvotes: 5
Views: 312
Reputation: 67778
You may try this. The order of date-time components and separators in format
should reflect those in the object to be converted. See also ?strptime
.
with(filesInside,
as.POSIXct(paste(Date, Time),
format = "%d:%m:%Y %H:%M:%S",
tz = "NZ"))
# [1] "2013-09-01 10:35:49 NZST" "2013-09-01 10:35:50 NZST"
Upvotes: 3
Reputation: 55340
library(lubridate)
dmy_hms(apply(filesInside, 1, paste, collapse=" "), tz="NZ")
# [1] "2013-09-01 10:35:49 NZST" "2013-09-01 10:35:50 NZST"
lubridate
here is a cinch, especially with its collection of dmy_hms
, ymd
etc functions.
To properly paste across rows, simply use apply(<data.frame>, 1, paste, collapse=" ")
Upvotes: 2