data.table

时间:2015-09-15 20:14:49

标签: r data.table posixct posixlt

我想从存储在R中的时间戳中提取日期作为POSIXct类。

e.g。

> str(wdt$v_timestamp)
POSIXct[1:15399], format: "2011-08-15 00:00:00" "2011-08-18 18:30:00" "2011-07-20 22:15:00" "2011-07-05 03:30:00" "2011-09-09 00:30:00" ...

当我进行转换时,它似乎绊倒了

> wdt[,v_date:=trunc(v_timestamp, "day")]
Error in `[.data.table`(wdt, , `:=`(v_date, trunc(v_timestamp, "day"))) : 
  (list) object cannot be coerced to type 'double'
In addition: Warning message:
In `[.data.table`(wdt, , `:=`(v_date, trunc(v_timestamp, "day"))) :
  Supplied 11 items to be assigned to 15399 items of column 'v_date' (recycled leaving remainder of 10 items).

我认为因为trunc会返回一个列表然后尝试回收。

然而这很有效。

> str(wdt$v_date) > wdt[,v_date:=as.POSIXct(trunc(v_timestamp, "day"))]
> str(wdt$v_date)
 POSIXct[1:15399], format: "2011-08-15" "2011-08-18" "2011-07-20" "2011-07-05" "2011-09-09" "2011-04-20" "2010-12-03" "2011-01-31" ...

我看到有关该列表的错误消息,但我并不理解。有没有更好的方法来做到这一点?

0 个答案:

没有答案