应用于data.table时,aaply函数中的错误

时间:2014-04-04 22:21:07

标签: r date data.table plyr

我的数据如下:

SUBJECT_Blinded LINE    MODULE  CENTRE_Blinded  STUDYPER    PACKID  SACDPDAT    SACDP1   SACRTDAT   SACRT1
1   1201001 8   SPX 1201    8   14096   2010-04-25  42  2011-06-22  0
2   1201001 4   SPX 1201    5   10615   2010-01-03  42  2011-01-31  9

输出输出(sac [1:2,]):

structure(list(SUBJECT_Blinded = c(1201001, 1201001), LINE = c(8, 
5), MODULE = c("SPX", "SPX"), CENTRE_Blinded = c(1201, 1201), 
STUDYPER = c(7, 4), PACKID = c(10096, 10595), SACDPDAT = structure(c(1335304800, 
1325545200), class = c("POSIXct", "POSIXt"), tzone = ""), 
SACDP1 = c(35, 35), SACRTDAT = structure(c(1340316000, 1327964400
), class = c("POSIXct", "POSIXt"), tzone = ""), SACRT1 = c(0, 
9)), .Names = c("SUBJECT_Blinded", "LINE", "MODULE", "CENTRE_Blinded", 
"STUDYPER", "PACKID", "SACDPDAT", "SACDP1", "SACRTDAT", "SACRT1"
), sorted = c("SUBJECT_Blinded", "PACKID"), class = c("data.table", 
"data.frame"), row.names = c(NA, -2L), .internal.selfref = <pointer: 0x0000000000300788>)

当我尝试执行命令时:

sac[,treatment_days := aaply(sac, 1, function(x){list(format(seq(from = x[7], to = x[10], by = "1 day"),"%Y-%m-%d"))})]
出现

错误:     错误:'from'的长度必须为1 为什么它会那样工作?

1 个答案:

答案 0 :(得分:0)

你可能正在寻找这样的东西:

 ## Note the use of list(list( . ))
 sac[  , treatment_days := list(list(seq(from=SACDPDAT, to= SACRTDAT)))
       , by=list(SACDPDAT, SACRTDAT)
     ]

几点:

  1. 使用list( list( ))返回一个本身就是列表的列。 (out list()用于data.table的语法)
  2. 您确定要列出列表吗?它符合您的最终目标吗?更高的桌子可能会好很多。特别是data.table可以快速访问特定的行,使用键
  3. 以上内容仍需格式化,by seq需要根据您的需求进行调整。