Converting datetime from character to POSIXct object
NickName:philiporlando Ask DateTime:2017-04-18T06:01:47

Converting datetime from character to POSIXct object

I have an instrument that exports data in an unruly time format. I need to combine the date and time vectors into a new datetime vector in the following POSIXct format: %Y-%m-%d %H:%M:%S. Out of curiosity, I attempted to do this in three different ways, using as.POSIXct(), strftime(), and strptime(). When using my example data below, only the as.POSIXct() and strftime() functions work, but I am curious as to why strptime() is producing NAs? Also, I cannot convert the strftime() output into a POSIXct object using as.POSIXct()...

When trying these same functions on my real data (of which I've only provided you with the first for rows), I am running into an entirely different problem. Only the strftime() function is working. For some reason the as.POSIXct() function is also producing NAs, which is the only command I actually need for converting my datetime into a POSIXct object...

It seems like there are subtle differences between these functions, and I want to know how to use them more effectively. Thanks!

Reproducible Example:

## Creating dataframe:
date <- c("2017-04-14", "2017-04-14","2017-04-14","2017-04-14")
time <- c("14:24:24.992000","14:24:25.491000","14:24:26.005000","14:24:26.511000")
value <- c("4.106e-06","4.106e-06","4.106e-06","4.106e-06")
data <- data.frame(date, time)
data <- data.frame(data, value) ## I'm sure there is a better way to combine three vectors...
head(data)

## Creating 3 different datetime vectors:

## This works in my example code, but not with my real data... 
data$datetime1 <- as.POSIXct(paste(data$date, data$time), format = "%Y-%m-%d %H:%M:%S",tz="UTC")
class(data$datetime1)

## This is producing NAs, and I'm not sure why:
data$datetime2 <- strptime(paste(data$date, data$time), format = "%Y-%m-%d %H:%M%:%S", tz = "UTC") 
class(data$datetime2)

## This is working just fine
data$datetime3 <- strftime(paste(data$date, data$time), format = "%Y-%m-%d %H:%M%:%S", tz = "UTC")
class(data$datetime3)
head(data)

## Since I cannot get the as.POSIXct() function to work with my real data, I tried this workaround. Unfortunately I am running into trouble...
data$datetime4 <- as.POSIXct(x$datetime3, format = "%Y-%m-%d %H:%M%:%S", tz = "UTC")

Link to real data: here

Example using real_data.txt:

## Reading in the file:
fpath <- "~/real_data.txt"
x <- read.csv(fpath, skip = 1, header = FALSE, sep = "", stringsAsFactors = FALSE)
names(x) <- c("date","time","bscat","scat_coef","pressure_mbar","temp_K","CH1","CH2") ## This is data from a Radiance Research Integrating Nephelometer Model M903 for anyone who is interested!

## If anyone could get this to work that would be awesome!
x$datetime1 <- as.POSIXct(paste(x$date, x$time), format = "%Y-%m-%d %H:%M%:%S", tz = "UTC") 

## This still doesn't work...
x$datetime2 <- strptime(paste(x$date, x$time), format = "%Y-%m-%d %H:%M%:%S", tz = "UTC") 

 ## This works: 
x$datetime3 <- strftime(paste(x$date, x$time), format = "%Y-%m-%d %H:%M%:%S", tz = "UTC")

## But I cannot convert from strftime character to POSIXct object, so it doesn't help me at all... 
x$datetime4 <- as.POSIXct(x$datetime3, format = "%Y-%m-%d %H:%M%:%S", tz = "UTC")    

head(x)

Solution:

I was not providing the as.POSIXct() function with the correct format string. Once I changed %Y-%m-%d %H:%M%:%S to %Y-%m-%d %H:%M:%S, the data$datetime2, data$datetime4, x$datetime1 and x$datetime2 were working properly! Big thanks to PhilC for debugging!

Copyright Notice:Content Author:「philiporlando」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43460329/converting-datetime-from-character-to-posixct-object

Answers
PhilC 2017-04-17T23:16:54

For your real data issue replace the %m% with %m:\n\n## Reading in the file:\nfpath <- \"c:/r/data/real_data.txt\"\nx <- read.csv(fpath, skip = 1, header = FALSE, sep = \"\", stringsAsFactors = FALSE)\nnames(x) <- c(\"date\",\"time\",\"bscat\",\"scat_coef\",\"pressure_mbar\",\"temp_K\",\"CH1\",\"CH2\") ## This is data from a Radiance Research Integrating Nephelometer Model M903 for anyone who is interested!\n\n## issue was the %m% - fixed\nx$datetime1 <- as.POSIXct(paste(x$date, x$time), format = \"%Y-%m-%d %H:%M:%S\", tz = \"UTC\") \n\n## Here too - fixed\nx$datetime2 <- strptime(paste(x$date, x$time), format = \"%Y-%m-%d %H:%M:%S\", tz = \"UTC\") \nhead(x)\n",


PhilC 2017-04-17T22:48:00

There was a format string error causing the NAs; try this:\n\n## This is no longer producing NAs:\ndata$datetime2 <- strptime(paste(data$date, data$time), format = \"%Y-%m-%d %H:%M:%S\",tz=\"UTC\") \nclass(data$datetime2)\n",


More about “Converting datetime from character to POSIXct object” related questions

Converting datetime from character to POSIXct object

I have an instrument that exports data in an unruly time format. I need to combine the date and time vectors into a new datetime vector in the following POSIXct format: %Y-%m-%d %H:%M:%S. Out of

Show Detail

How to preserve decimal values when converting POSIXct to character?

&gt; t &lt;- Sys.time() &gt; ct &lt;- as.character(t) &gt; t - as.POSIXct(ct) Time difference of 0.4370408 secs The above example indicates that precision is lost when converting POSIXct to charac...

Show Detail

NA result in converting string to POSIXct date time in R

Uploading csv file and converting a datetime from character to POSIXct I stumbled in a unexpected NA result: as.POSIXct(&quot;20210328 02:00:00&quot;, format = &quot;%Y%m%d %H:%M:%S&quot;) this see...

Show Detail

append a datetime object on POSIXct

I need to append a datetime object to my POSIXct element. Some sampledata: my_chr&lt;-c('2017-02-19 06:00','2017-03-10 06:00','2017-04-15 06:00') myPSX&lt;-as.POSIXct(my_chr,format='%Y-%m-%d %H:%M...

Show Detail

Converting a Date object to POSIXct with timezones

As described here and in other questions, it's fairly straight-forward to convert a Date object to POSIXct -- with the gotcha from ?as.POSIXct that dates without times are treated as being midnight...

Show Detail

Convert 14 digit datetime object to POSIXct

I have an array of datetime objects created in python, that I'd like to read into R as a POSIXct. For example, the 14 digit datetime object looks like: 736908.51782407 I believe this is the datet...

Show Detail

Converting numeric time to datetime POSIXct format in R

I have a data frame containing what should be a datetime column that has been read into R. The time values are appearing as numeric time as seen in the below data example. I would like to convert t...

Show Detail

Converting numeric time to datetime POSIXct format in R

I have a data frame containing what should be a datetime column that has been read into R. The time values are appearing as numeric time as seen in the below data example. I would like to convert t...

Show Detail

rbind converts POSIXct to character - How to convert back to POSIXct?

Despite using the stringsASFactors = FALSE argument, rbind() is converting my POSIXct vectors into class character. I've checked out discussions on cbind/rbind type functions and how they handle PO...

Show Detail

How to automatically recognise timezone when converting from a datetime saved as character to POSIXct in R?

I'm trying to save a POSIXct datetime as a string with timezone, then convert it back to the original POSIXct datetime with the correct timezone. In the back-conversion, I'm wanting the timezone to...

Show Detail