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
R provides several options for dealing with date and date/time data. The builtin as. Date function handles dates (without times); the contributed library chron handles dates and times, but does not control for time zones; and the POSIXct and POSIXlt classes allow for dates and times with control for time zones.
Lubridate is an R package that makes it easier to work with dates and times.
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"
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=" ")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With