2

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

BeckyF
  • 91
  • 6
  • 2
    `lubridate::mdy_hms(paste(data$Date, data$Time))` – d.b Jul 30 '19 at 15:11
  • Can you give us a sample of your data using `dput`? Especially the parts that aren't parsing properly are important. If it works most, but not all, of the time then there might be some quirk in your data. –  Jul 30 '19 at 15:18
  • Thanks, @d.b That solution works! – BeckyF Jul 30 '19 at 18:49

1 Answers1

1
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" 
> 
Petr Matousu
  • 3,120
  • 1
  • 20
  • 32