Search code examples
rstrptime

Input string too long error if using strptime in R


My file contains a list of timestamps:

Fri Feb 14 19:07:31 +0000 2014
Fri Feb 14 19:07:46 +0000 2014
Fri Feb 14 19:07:50 +0000 2014
Fri Feb 14 19:08:04 +0000 2014

and reading it into R using:

dataset <- read.csv(file="Data.csv")

and i then write R commands to enable R to detect the timestamps:

time <- strptime(dataset,format = "%a %b %d %H:%M:%S %z %Y", tz = "GMT")

but I'm constantly getting an error saying:

Error in strptime(dataset, format = "%a %b %d %H:%M:%S %z %Y") : 
input string is too long

it was working well at first but after i added:

defaults write org.R-project.R force.LANG en_US.UTF-8

in my terminal to fix some preferences in R for mac os x, the timestamp command stopped working an keep producing the error i mentioned above.


Solution

  • This is your original Data. myDates as Character.

    dtData<-data.frame(myDates=c( "Fri Feb 14 19:07:31 +0000 2014",
                      "Fri Feb 14 19:07:46 +0000 2014",
                      "Fri Feb 14 19:07:50 +0000 2014",
                      "Fri Feb 14 19:08:04 +0000 2014"))
    > dtData
                             myDates
    1 Fri Feb 14 19:07:31 +0000 2014
    2 Fri Feb 14 19:07:46 +0000 2014
    3 Fri Feb 14 19:07:50 +0000 2014
    4 Fri Feb 14 19:08:04 +0000 2014
    

    you need to select dtData$myDates column

    time <- strptime(dtData$myDates,format = "%a %b %d %H:%M:%S %z  %Y", tz = "GMT");time
    
    [1] "2014-02-14 19:07:31 GMT" "2014-02-14 19:07:46 GMT"
    [3] "2014-02-14 19:07:50 GMT" "2014-02-14 19:08:04 GMT"