1

我正在尝试按列拆分data.table(增强的data.framePOSIXct,但没有成功...

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
4

1 回答 1

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)
于 2013-04-19T18:28:29.477 回答