APW APW - 1 year ago 108
R Question

Converting from fctr to date format.

I am attempting to convert a column in my data set from fctr to date format. The current column has data formatted as follows: "01/01/14. 01:00 Am." Ideally I would like to create a column for day and then a column for time as well. There are periods following the day and the time which is another issue I am facing. So far I have attempted to use lubridate to create a new column of data but I get the error "All formats failed to parse. No formats found." Any help would be greatly appreciated, thank you.

test <- fourteen %>%
mutate(When = mdy_hms(V3))

Answer Source

If your date factor literally has levels that look like 01/01/14. 01:00 Am. including two periods and a space between the first period and the first hour digits and a space between the minutes and the am/pm designation, and all the dates are in this format, then the following should work:

... mutate(When = as.POSIXct(V3, format="%m/%d/%y. %H:%M %p.")) ...

In particular, the following standalone testcase works fine:

as.POSIXct(factor("01/01/14. 01:00 Am."), format="%m/%d/%y. %H:%M %p.")

For more information on the format argument being used here, see the R help page for the function strftime.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download