我有一个大型数据表,其中包含每个ID的事件的开始和结束日期:
library(data.table)
dt = data.table(
ID = c(1,1,2,2),
STARTDATE = as.Date(c("2011-10-10","2011-10-13","2011-10-10","2011-10-13"),format = "%Y-%m-%d"),
ENDDATE = as.Date(c("2011-10-12","2011-10-15","2011-10-12","2011-10-15"),format = "%Y-%m-%d")
)
dt
> ID STARTDATE ENDDATE
>1: 1 2011-10-10 2011-10-12
>2: 1 2011-10-13 2011-10-15
>3: 2 2011-10-10 2011-10-12
>4: 2 2011-10-13 2011-10-15
我想在时间窗口中为每个ID和日添加行到此数据表,其结果如下:
STARTDATE ENDDATE ID DAILY
1: 2011-10-10 2011-10-12 1 2011-10-10
2: 2011-10-10 2011-10-12 1 2011-10-11
3: 2011-10-10 2011-10-12 1 2011-10-12
4: 2011-10-13 2011-10-15 1 2011-10-13
5: 2011-10-13 2011-10-15 1 2011-10-14
6: 2011-10-13 2011-10-15 1 2011-10-15
7: 2011-10-10 2011-10-12 2 2011-10-10
8: 2011-10-10 2011-10-12 2 2011-10-11
9: 2011-10-10 2011-10-12 2 2011-10-12
10: 2011-10-13 2011-10-15 2 2011-10-13
11: 2011-10-13 2011-10-15 2 2011-10-14
12: 2011-10-13 2011-10-15 2 2011-10-15
我的代码如下:
dt[, cbind(.SD, seq(STARTDATE, ENDDATE, 1)), by = list(STARTDATE, ENDDATE)]
但它不会产生想要的结果:
STARTDATE ENDDATE ID V2
1: 2011-10-10 2011-10-12 1 2011-10-10
2: 2011-10-10 2011-10-12 2 2011-10-11
3: 2011-10-10 2011-10-12 1 2011-10-12
4: 2011-10-13 2011-10-15 1 2011-10-13
5: 2011-10-13 2011-10-15 2 2011-10-14
6: 2011-10-13 2011-10-15 1 2011-10-15
Warnmeldungen:
1: In data.table::data.table(...) :
Item 1 is of size 2 but maximum size is 3 (recycled leaving remainder of 1 items)
2: In data.table::data.table(...) :
Item 1 is of size 2 but maximum size is 3 (recycled leaving remainder of 1 items)
它需要某处的ID,但我无法将其输入到数据表的by
部分。它给出了另一个错误。有任何想法吗?
答案 0 :(得分:3)
这是一个选项。请注意,我们可以使用 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.textView);
tv.setText("Starting app...");
new Thread(new Runnable() {
@Override
public void run() {
DoIt();
}
}).start();
}
private void DoIt() {
HttpClient httpClient = new DefaultHttpClient();
try {
for (int i=1; i<100; i++) {
HttpPost request2 = new HttpPost("http://192.168.1.12:3000/bytes/data" + i);
HttpResponse response2 = httpClient.execute(request2);
// Do something with data. In some cases, it has to download 1MB data
}
}
// catch + finally ...
}
指定每行的分组,从而生成一个名为by = 1:nrow(dt)
的新列。然后,我们可以使用nrow
删除该列。
[, nrow := NULL]