0
votes

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")
4
Funny! Were the 3:04 et al. in your question earlier? I seem to have missed those!asb

4 Answers

0
votes

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")
0
votes

Have you tried:

R> as.Date(c("6/2/2013", "10/21/2013"), format="%m/%d/%Y")
[1] "2013-06-02" "2013-10-21"

or

R> as.POSIXct(c("6/2/2013", "10/21/2013"), format="%m/%d/%Y")
[1] "2013-06-02 IST" "2013-10-21 IST"
0
votes

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")
0
votes

Try as.Date("6/2/2013", format="%m/%d/%Y"). But mind that you will get the same result even if your "irregular" character is "6/2/2013abcd"