I am a R newbie! I am trying to convert irregular characters to dates and at the same time drop the time, but keep getting NA, For example:
test = as.POSIXct(strptime("6/2/2013 10:27","10/21/2013 3:04"),format="%m/%d/%Y %H:%M")
I am a R newbie! I am trying to convert irregular characters to dates and at the same time drop the time, but keep getting NA, For example:
test = as.POSIXct(strptime("6/2/2013 10:27","10/21/2013 3:04"),format="%m/%d/%Y %H:%M")
I'm not sure what strptime is supposed to do, but if you just replace it with c() then your function works fine. It would also work with as.Date
test = as.POSIXct(c("6/2/2013 10:27","10/21/2013 3:04"),format="%m/%d/%Y %H:%M")
test = as.Date(c("6/2/2013 10:27","10/21/2013 3:04"),format="%m/%d/%Y %H:%M")
If you start dealing with dates in R, I urge you to try the lubridate
package, it simplify a lot this kind of operation.
require(lubridate)
date_string <- c("6/2/2013", "10/21/2013")
mdy(date_string)
[1] "2013-06-02 UTC" "2013-10-21 UTC"
For example here, in the function mdy
, m
stand for month, d
for day and y
for year and all the others functions are that intuitive.
Check the vignette to have a better idea of its possibilities
vignette("lubridate", package = "lubridate")