我有一个data.table对象,我根据日期变量id
和start.time
按stop.time
展开。下面列出的代码可行 - 我的问题是如何使我的代码更容易使用。
如何避免在声明中手动列出data.table对象的现有变量,即&#34; start.time&#34;和&#34; stop.time&#34;在test <- DT[, list(start.time, stop.time, time=seq(start.time, stop.time, by="1 month")), by=id]
。如果你假设我不仅有两个变量而是几十个变量,那么手动添加变量会很烦人。目的是包括原始data.table对象中存在的所有列。
我尝试添加.SD
和setdiff(names(DT), "id")
而不是变量名称,但这会引发错误。
DT <- data.table(id=sample(1000),
start.time=sample(seq(as.Date("01-01-2010", format="%d-%m-%Y"), as.Date("31-12-2010", format="%d-%m-%Y"), by="day"), 1000, replace=TRUE),
stop.time=sample(seq(as.Date("01-01-2011", format="%d-%m-%Y"), as.Date("31-12-2011", format="%d-%m-%Y"), by="day"), 1000, replace=TRUE))
# > DT
# id start.time stop.time
# 1: 105 2010-12-16 2011-07-30
# 2: 837 2010-01-15 2011-01-19
# 3: 155 2010-11-30 2011-10-07
# 4: 125 2010-03-31 2011-04-19
# 5: 192 2010-07-13 2011-03-27
# ---
# 996: 749 2010-07-19 2011-02-18
# 997: 660 2010-06-28 2011-10-25
# 998: 808 2010-03-22 2011-12-04
# 999: 291 2010-09-03 2011-06-08
# 1000: 741 2010-01-25 2011-01-23
test <- DT[, list(start.time, stop.time, time=seq(start.time, stop.time, by="1 month")), by=id]
# > test
# id start.time stop.time time
# 1: 903 2010-11-08 2011-10-22 2010-11-08
# 2: 903 2010-11-08 2011-10-22 2010-12-08
# 3: 903 2010-11-08 2011-10-22 2011-01-08
# 4: 903 2010-11-08 2011-10-22 2011-02-08
# 5: 903 2010-11-08 2011-10-22 2011-03-08
# ---
# 12346: 229 2010-09-11 2011-10-02 2011-05-11
# 12347: 229 2010-09-11 2011-10-02 2011-06-11
# 12348: 229 2010-09-11 2011-10-02 2011-07-11
# 12349: 229 2010-09-11 2011-10-02 2011-08-11
# 12350: 229 2010-09-11 2011-10-02 2011-09-1
答案 0 :(得分:4)
您可以像这样使用.SD
:
DT[, c(.SD, list(time=seq(start.time, stop.time, by="1 month"))), by=id]