Search code examples
rdatetimeanytime

Creating a POSIX from a character


I am trying to make my water quality sensor data into time-series data for analysis in R. There is one column with date (format m/dd/yyyy) and another with time (hh:mm:ss)

I have managed to paste them together into a character vector and then attempted to use the anytime function to convert the DateTime to POSIXct format.

data$DateTime <- as.character(paste(data$Date, data$Time))

data$DateTime2 <- anytime(as.character(data$DateTime))

The above code works for some of my data but not all of the long time series. It creates NAs for some DateTimes, and converts other periods to all 00:00:00 but on the correct date.

I have also tried strptime and as.POSIXct functions, but both of those do not recognize the input formats. and makes all DateTimes NAs


Solution

  • as.POSIXct(strptime(paste("12/30/2019","05:45:00"),format="%m/%d/%Y %T"))
    [1] "2019-12-30 05:45:00 CET"
    class(as.POSIXct(strptime(paste("12/30/2019","05:45:00"),format="%m/%d/%Y %T")))
    [1] "POSIXct" "POSIXt" 
    >