申请data.table来查找间隔的交叉长度

时间:2014-04-03 21:49:48

标签: r date data.table posixct

我有这样的数据:

View(dose_merged)

    SUBJECT_Blinded PACKID  SACDPDAT    SACRTDAT    treatment_interval  SD_SDAT     SD_EDAT
1   1501301         10094   2012-05-26  2012-07-23  58                  2012-01-03  2013-01-02
2   1601301         10555   2012-01-03  2012-01-31  28                  2012-01-03  2013-01-0

使用数据表中的列类型:

> mapply(class, dose_merged)
$SUBJECT_Blinded
[1] "numeric"

$PACKID
[1] "numeric"

$SACDPDAT
[1] "POSIXct" "POSIXt" 

$SACRTDAT
[1] "POSIXct" "POSIXt" 

$treatment_interval
[1] "Interval"
attr(,"package")
[1] "lubridate"

$SD_SDAT

[1] "POSIXct" "POSIXt" 

$SD_EDAT
[1] "POSIXct" "POSIXt" 

我想确定间隔的交叉长度:间隔(SACDPDAT,SACRTDAT)和间隔(SD_SDAT,SD_EDAT)。 我正在尝试这个:

dose_merged[,intersect1 := aaply(dose_merged, 1, function(x){intersect(interval(x[3],x[4]),      interval(x[8],x[9]))})]

但后来我收到错误消息:

Error: error while computing 'x' when choosing method for 'intersect': Error in as.POSIXct.default(start) : 
  do not know how to convert 'start' to class “POSIXct”

该行

intersect(interval(x[3],x[4]), interval(x[8],x[9]))})

适用于指定的行x。 我有什么想法吗?

前两行dput(dose_merge):

structure(list(SUBJECT_Blinded = c(1101001, 1101001), PACKID = c(10096, 
10595), SACDPDAT = structure(c(1335304800, 1325545200), class = c("POSIXct", 
"POSIXt"), tzone = ""), SACRTDAT = structure(c(1340316000, 1327964400
), class = c("POSIXct", "POSIXt"), tzone = ""), treatment_interval = structure(c(58, 
28), class = structure("Interval", package = "lubridate")), TS_SDAT = structure(c(NA_real_, 
NA_real_), class = c("POSIXct", "POSIXt"), tzone = ""), TS_EDAT = structure(c(NA_real_, 
NA_real_), class = c("POSIXct", "POSIXt"), tzone = ""), SD_SDAT = structure(c(1325545200, 
1325545200), class = c("POSIXct", "POSIXt"), tzone = ""), SD_EDAT = structure(c(1357081200, 
1357081200), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = c("SUBJECT_Blinded", 
"PACKID", "SACDPDAT", "SACRTDAT", "treatment_interval", "TS_SDAT", 
"TS_EDAT", "SD_SDAT", "SD_EDAT"), sorted = "SUBJECT_Blinded", class = c("data.table", 
"data.frame"), row.names = c(NA, -2L), .internal.selfref = <pointer: 0x0000000002f30788>)

0 个答案:

没有答案