6

我正在阅读一个表格,它包含描述时间戳的字符串。我只想从字符串转换为内置的日期时间类型......

R> Q <- read.table(textConnection('
               tsstring
1 "2009-09-30 10:00:00"
2 "2009-09-30 10:15:00"
3 "2009-09-30 10:35:00"
4 "2009-09-30 10:45:00"
5 "2009-09-30 11:00:00"
'), as.is=TRUE, header=TRUE)
R> ts <- strptime(Q$tsstring, "%Y-%m-%d %H:%M:%S", tz="UTC")

如果我尝试将 datetime 列存储到 data.frame 中,我会收到一个奇怪的错误:

R> Q$ts <- ts
Error in `$<-.data.frame`(`*tmp*`, "ts", value = list(sec = c(0, 0, 0,  : 
  replacement has 9 rows, data has 5

但是如果我通过data.frame中保存的数字表示,它可以工作......

R> EPOCH <- strptime("1970-01-01 00:00:00", "%Y-%m-%d %H:%M:%S", tz="UTC")
R> Q$minutes <- as.numeric(difftime(ts, EPOCH, tz="UTC"), units="mins")
R> Q$ts <- EPOCH + 60*Q$minutes

对了解情况有什么帮助吗?

4

1 回答 1

12

strptime返回类POSIXlt,你需要POSIXct在数据框中:

R> class(strptime("2009-09-30 10:00:00", "%Y-%m-%d %H:%M:%S", tz="UTC"))
[1] "POSIXt"  "POSIXlt"
R> class(as.POSIXct("2009-09-30 10:00:00", "%Y-%m-%d %H:%M:%S", tz="UTC"))
[1] "POSIXt"  "POSIXct"

POSIXct将自 1970 年初以来的(有符号)秒数表示为数字向量。类POSIXlt是一个命名的向量列表,表示 sec、min、hour、mday、mon、year 等。

R> unclass(strptime("2009-09-30 10:00:00", "%Y-%m-%d %H:%M:%S", tz="UTC"))
$sec
[1] 0
$min
[1] 0
$hour
[1] 10
$mday
[1] 30
$mon
[1] 8
$year
[1] 109
$wday
[1] 3
$yday
[1] 272
$isdst
[1] 0
attr(,"tzone")
[1] "UTC"

R> unclass(as.POSIXct("2009-09-30 10:00:00", "%Y-%m-%d %H:%M:%S", tz="UTC"))
[1] 1.254e+09
attr(,"tzone")
[1] "UTC"
于 2009-11-26T13:23:23.930 回答