【问题标题】:Input string too long error if using strptime in R如果在 R 中使用 strptime,则输入字符串太长错误
【发布时间】:2017-10-15 02:09:28
【问题描述】:

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

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:

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

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

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

一开始还不错,但是在我添加之后:

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

在我的终端中为 mac os x 修复 R 中的一些首选项时,timestamp 命令停止工作并继续产生我上面提到的错误。

【问题讨论】:

标签: r strptime


【解决方案1】:

这是您的原始数据。 myDates 作为字符。

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"

【讨论】:

    猜你喜欢
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    相关资源
    最近更新 更多