Home > Net >  Convert string to timestamps in R
Convert string to timestamps in R

Time:10-11

I have the string which is formatted as below:

Tue Feb 11 12:28:36  0000 2014

I try to convert this string to timestamps in R by using:

timeobj <- strptime(df[1], format = "%a %b %e %H:%M:%S %z %Y", tz = "GMT")

where df[1] is in format of Tue Feb 11 12:28:36 0000 2014

However, I got an error as below:

Error in strptime(df[1], format = "%a %b %e %H:%M:%S %z %Y", tz = "GMT") : 
  input string is too long

How can I fix this?

dput(df[ 1:5, 1]) =

c("Tue Feb 11 12:47:26 0000 2014", "Tue Feb 11 12:55:09 0000 2014", "Tue Feb 11 13:22:29 0000 2014", "Tue Feb 11 13:24:31 0000 2014", "Tue Feb 11 13:34:00 0000 2014")

CodePudding user response:

It looks like that your locale is not fitting the abbreviated weekday and month name.

x <- c("Tue Feb 11 12:47:26  0000 2014",
  "Tue Feb 11 12:55:09  0000 2014", "Tue Feb 11 13:22:29  0000 2014",
  "Tue Feb 11 13:24:31  0000 2014", "Tue Feb 11 13:34:00  0000 2014")

Sys.setlocale("LC_ALL", "de_AT.UTF-8")
strptime(x, format = "%a %b %e %H:%M:%S %z %Y", tz = "GMT")
#[1] NA NA NA NA NA

Sys.setlocale("LC_ALL", "C")
strptime(x, format = "%a %b %e %H:%M:%S %z %Y", tz = "GMT")
#[1] "2014-02-11 12:47:26 GMT" "2014-02-11 12:55:09 GMT"
#[3] "2014-02-11 13:22:29 GMT" "2014-02-11 13:24:31 GMT"
#[5] "2014-02-11 13:34:00 GMT"

The manual of strptime says: '%a' Abbreviated weekday name in the current locale on this platform.

Also it looks like you are providing a data.frame with df[1] and not a vector which can probably provided with df[,1].

CodePudding user response:

With me, it runs with no error. Perhaps this is an issue with your df structure.

  • Related