I want to extract the date from a timestamp stored in R as a POSIXct class.
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" ...
When I do the conversion, it seems to stumble
> 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).
I think because trunc
is return a list which it then attempts to recycle.
This works however.
> 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" ...
I see the error message about the list but I don't really understand. And is there a better way to have done this?