Error creating R data.table with date-time POSIXlt -
problem creating data.table date-time column:
> mdt <- data.table(id=1:3, d=strptime(c("06:02:36", "06:02:48", "07:03:12"), "%h:%m:%s")) > class(mdt) [1] "data.table" "data.frame" > print(mdt) error in `rownames<-`(`*tmp*`, value = paste(format(rn, right = true), : length of 'dimnames' [1] not equal array extent enter frame number, or 0 exit 1: print(list(id = 1:3, d = list(sec = c(36, 48, 12), min = c(2, 2, 3), hour = c(6, 6, 7), mday = c(31, 2: print.data.table(list(id = 1:3, d = list(sec = c(36, 48, 12), min = c(2, 2, 3), hour = c(6, 6, 7), m 3: `rownames<-`(`*tmp*`, value = paste(format(rn, right = true), ":", sep = ""))
create data.frame , convert data.table works!
> mdf <- data.frame(id=1:3, d=strptime(c("06:02:36", "06:02:48", "07:03:12"), "%h:%m:%s")) > print(mdf) id d 1 1 2014-01-31 06:02:36 2 2 2014-01-31 06:02:48 3 3 2014-01-31 07:03:12 > mdt <- as.data.table(mdf) > print(mdt) id d 1: 1 2014-01-31 06:02:36 2: 2 2014-01-31 06:02:48 3: 3 2014-01-31 07:03:12 > class(mdt) [1] "data.table" "data.frame"
am missing or bug? if bug, report it?
note use r version 3.0.0 , see warnings re. packages built version 3.0.2. can problem? should upgrade r itself? else seems working though.
formatting response blue magister's comment (thanks much), data.table not support posixlt data types performance reason -- see cast string idatetime suggested possible duplicate.
so way go cast time itime (type provided data.table) or date-time (or date only) posixct, depending upon whether date info important or not:
> mdt <- data.table(id=1:3, d=as.itime(strptime(c("06:02:36", "06:02:48", "07:03:12"), "%h:%m:%s"))) > print(mdt) id d 1: 1 06:02:36 2: 2 06:02:48 3: 3 07:03:12 > mdt <- data.table(id=1:3, d=as.posixct(strptime(c("06:02:36", "06:02:48", "07:03:12"), "%h:%m:%s"))) > print(mdt) id d 1: 1 2014-01-31 06:02:36 2: 2 2014-01-31 06:02:48 3: 3 2014-01-31 07:03:12
as note in case can benefit it, wanted create date & time input data date & time in separate fields. found useful learn (see ?itime) 1 can add time itime date-time posixct , date-time posixct follows:
> mdt <- as.posixct("2014-01-31") + as.itime("06:02:36") > print(mdt) [1] "2014-01-31 06:02:36 est" > class(mdt) [1] "posixct" "posixt"
Comments
Post a Comment