如果在R中使用strptime,则输入字符串太长错误

时间:2017-10-15 02:09:28

标签: r strptime

我的文件包含时间戳列表:

index.html

并使用以下方法将其读入R:

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

然后我编写R命令以使R能够检测时间戳:

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

但我经常收到错误说:

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

起初它运作良好,但在我添加之后:

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

在我的终端中修改了一些偏好在R中为mac os x,timestamp命令停止工作,继续产生我上面提到的错误。

1 个答案:

答案 0 :(得分:0)

这是您的原始数据。 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

您需要选择dtData $ myDates列

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"