我在读取一个表格,其中包含描述时间戳的字符串。我只想把它从字符串转换为内置的日期时间类型...
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")
如果我试图将日期时间列存储到数据框中,会出现一个奇怪的错误:
R> Q$ts <- ts
Error in `$<-.data.frame`(`*tmp*`, "ts", value = list(sec = c(0, 0, 0, :
replacement has 9 rows, data has 5
但是,如果我通过数据框中保存的数字表示进行操作,它就可以工作...
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
需要帮助理解情况吗?