0

I got a long text file with multiple columns of which the first contains the time stamp. The format is like that: 2016-02-19T06:27:48

I tried convertig this to a POSIXct format:

test1 <- "2016-02-19T06:27:48"
z <- as.POSIXct(test1,format="%Y-%m-%dT%H:%M:%S")
> str(z)
 POSIXct[1:1], format: "2016-02-19 06:27:48"

Which works as intended. Now doing this in a loop won't work, the respective column won't convert to a POSIXct format, but remains as character (thus won't work). Here some reproducible code:

test <- c("2016-02-19T06:27:48",
"2016-03-19T06:27:48",
"2016-02-19T12:27:48",
"2016-02-19T06:45:48",
"2016-03-19T12:27:48")
test <- data.frame(test, stringsAsFactors = FALSE)

for(i in 1:nrow(test)) {
  test$test[i] <- as.POSIXct(test$test[i], format = "%Y-%m-%d %H:%M:%S")
}
print(test)

Any hints? I think this can't be a big issue, but I can't find a solution that works even after browsing stackoverflow for some hours now. Thanks!

GeoEki
  • 437
  • 1
  • 7
  • 20

0 Answers0