【发布时间】:2020-04-11 22:24:01
【问题描述】:
我有一个数据集如下:
DT <- fread(
"ID country year Event_A Event_B
4 BEL 2002 0 1
5 BEL 2002 0 1
6 NLD 2002 1 1
7 NLD 2006 1 0
8 NLD 2006 1 1
9 GBR 2001 0 1
10 GBR 2001 0 0
11 GBR 2001 0 1
12 GBR 2007 1 1
13 GBR 2007 1 1",
header = TRUE)
我想只保留我在两个国家/地区观察到的观察结果。所以,BEL 将退出,因为它只有 2002 年的观察结果。
我想做类似DT[,if(unique(year)>1) .SD, by=country] 的事情,但这并没有做任何事情。我也试过DT[unique(year)>1, .SD, by=country],但这给出了错误:
Error in `[.data.table`(DT, unique(year) > 1, .SD, by = country) :
i evaluates to a logical vector length 4 but there are 10 rows. Recycling of logical i is no longer allowed as it hides more bugs than is worth the rare convenience. Explicitly use rep(...,length=.N) if you really need to recycle.
期望的输出:
DT <- fread(
"ID country year Event_A Event_B
6 NLD 2002 1 1
7 NLD 2006 1 0
8 NLD 2006 1 1
9 GBR 2001 0 1
10 GBR 2001 0 0
11 GBR 2001 0 1
12 GBR 2007 1 1
13 GBR 2007 1 1",
header = TRUE)
【问题讨论】:
标签: r data.table subset