Convert date character to POSIXct

Hello Everyone.

I am importing data from a CSV file that has a date column. When I import the data and use the str() command, I get the following:

str(mdata)
$ date: chr "2020/8/14 12:09:00" "2020/8/14 12:10:00" "2020/8/14 12:11:00" "2020/8/14 12:12:00" ...

I want to convert the data to POSIXct instead of chr.

When I use the following command:
mdata$date <- as.POSIXct(farmdata$date, format = "%Y-%m-%d %H:%M:%S", tx = "GMT")

This is what I get

str(mdata)
$ date: POSIXct, format: NA NA NA NA ..

Any idea why the dates are converted to NA?

Thank you,
Steve

Your data have slashes between the components of the dates so your format argument should have the same. Also, notice that I changed the tx argument to be tz.

as.POSIXct(farmdata$date, format = "%Y/%m/%d %H:%M:%S", tz = "GMT")
1 Like

Thank you! That worked!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.