通过POSIXct因子拆分data.table(或框架)

时间:2013-04-19 17:12:23

标签: r split dataframe data.table posixct

我正在尝试将data.table(增强型data.frame)拆分为POSIXct列,但未成功...

rangedt <- as.POSIXct(c("2012-10-01 06:00","2012-10-01 21:00"), tz='GMT'); N=1e2
dts <- as.POSIXct(runif(n=N, min=min(rangedt), max=max(rangedt)), tz='GMT', origin='1970-01-01')
DT <- data.table(x=rnorm(N), dts=dts) # put data.frame if you prefer
#                          x                        dts
#1:  0.938973900218328494383 2012-10-01 17:11:46.503828
#2:  0.582959687387282210480 2012-10-01 17:33:24.203815
#3: -1.492752410394331263888 2012-10-01 08:37:37.585960
#4:  0.677074458537853418605 2012-10-01 08:55:04.598939
#5:  0.012120685348577473275 2012-10-01 09:35:16.664197
#6: -1.353204371844073161668 2012-10-01 18:45:46.737178 
f <- cut(rangedt, breaks='10 min');
f
#[1] 2012-10-01 06:00:00 2012-10-01 21:00:00
#91 Levels: 2012-10-01 06:00:00 2012-10-01 06:10:00 2012-10-01 06:20:00

DT.split <- split(DT, f=findInterval(DT$dts,f))

length(DT.split)
#[1] This is because R make one class only fron the data, which I do not understand

1 个答案:

答案 0 :(得分:0)

我在这里发现了问题,因素和POSIXct不能很好地协同工作

rangedt <- as.POSIXct(c("2012-10-01 06:00","2012-10-01 21:00"), tz='GMT'); N=1e2
dts <- as.POSIXct(runif(n=N, min=min(rangedt), max=max(rangedt)), tz='GMT', origin='1970-01-01')
DT <- data.table(x=rnorm(N), dts=dts) # put data.frame if you prefer
#                          x                        dts
#1:  0.938973900218328494383 2012-10-01 17:11:46.503828
#2:  0.582959687387282210480 2012-10-01 17:33:24.203815
#3: -1.492752410394331263888 2012-10-01 08:37:37.585960
#4:  0.677074458537853418605 2012-10-01 08:55:04.598939
#5:  0.012120685348577473275 2012-10-01 09:35:16.664197
#6: -1.353204371844073161668 2012-10-01 18:45:46.737178 
f <- cut(rangedt, breaks='10 min');
f
#[1] 2012-10-01 06:00:00 2012-10-01 21:00:00
#91 Levels: 2012-10-01 06:00:00 2012-10-01 06:10:00 2012-10-01 06:20:00
f <- as.POSIXct(levels(f), tz='GMT', origin='1970-01-01')
DT[, groups:=findInterval(dts,f)]
DT.split <- split(DT, DT$groups)