I am having a problem with date & time format in R.
My R code is having the format: "%Y%m%d %H%M%OS". For example: "20170929 20:59:56.149"
Time in my case is character variable.
I am trying to write a R code where type of format will be converted into the format "%Y-%m-%d %H:%M:%OS"
(data <- strptime(data$time,
format = "%Y-%m-%d %H:%M:%OS",
tz = "GMT"))
or for example
data$time <- as.POSIXct(data$time, format = "%Y-%m-%d %H:%M:%OS")
But, I am getting NA. What could this be caused by? Do you have any idea how to fix it? Thank you in advance for your help!
From the manual
help(strftime): If the specified time is invalid (for example ‘"2010-02-30 08:00"’) all the components of the result are ‘NA’.Furthermore, if you want to manipulate your output format of a date you need the date stored as a Date-class (illustration of @izyda's comments).
First, reformat your date to make it easier to manipulate:
Then, change the class to Date:
Finally, choose your output format: