Select 数据框中的行根据另一个向量,包括重复
Select rows from a data frame according to another vector, including repetitions
示例数据:
dates=seq(as.POSIXct("2015-01-01 00:00:00"), as.POSIXct("2015-01-07 00:00:00"), by="day")
data=rnorm(7,1,2)
groupID=c(12,14,16,24,35,46,54)
DF=data.frame(Date=dates,Data=data,groupID=groupID)
BB=c(12,12,16,24,35,35)
DF[DF$groupID %in% BB,]
Date Data groupID
1 2015-01-01 4.4104202 12
3 2015-01-03 2.1557735 16
4 2015-01-04 -0.9880946 24
5 2015-01-05 -0.3396025 35
我需要根据向量 BB
中与 groupID 列匹配的值过滤数据框 DF
。但是,如果 BB
包含重复项,则不会反映在结果中。
由于我的向量 BB
包含两个值 1 和两个值 5,因此输出实际上应该是:
Date Data groupID
1 2015-01-01 4.4104202 12
1 2015-01-01 4.4104202 12
3 2015-01-03 2.1557735 16
4 2015-01-04 -0.9880946 24
5 2015-01-05 -0.3396025 35
5 2015-01-05 -0.3396025 35
有办法实现吗?并尽可能保持向量的顺序 BB
?
使用match()
(or findInterval()
):
DF[match(BB,DF$groupID),];
## Date Data groupID
## 1 2015-01-01 1.2199835 12
## 1.1 2015-01-01 1.2199835 12
## 3 2015-01-03 1.8141556 16
## 4 2015-01-04 0.2748579 24
## 5 2015-01-05 3.2030200 35
## 5.1 2015-01-05 3.2030200 35
(注意Data
栏是不同的,因为你使用rnorm()
生成它没有先调用set.seed()
。建议在任何代码中调用set.seed()
将随机性纳入其中的示例,以便可以重现准确的结果。)
你可以把BB
变成一个data.frame
,然后用merge()
把DF
和BB
按照它们的groupID
合并,就可以了具体点:
dates=seq(as.POSIXct("2015-01-01 00:00:00"), as.POSIXct("2015-01-07 00:00:00"), by="day")
groupID=c(12,14,16,24,35,46,54)
set.seed(1234)
data=rnorm(7,1,2)
DF=data.frame(Date=dates,Data=data,groupID=groupID)
BB=data.frame(groupID=c(12,12,16,24,35,35))
测试结果:
>merge(DF,BB,by="groupID")
groupID Date Data
1 12 2015-01-01 -1.414131
2 12 2015-01-01 -1.414131
3 16 2015-01-03 3.168882
4 24 2015-01-04 -3.691395
5 35 2015-01-05 1.858249
6 35 2015-01-05 1.858249
示例数据:
dates=seq(as.POSIXct("2015-01-01 00:00:00"), as.POSIXct("2015-01-07 00:00:00"), by="day")
data=rnorm(7,1,2)
groupID=c(12,14,16,24,35,46,54)
DF=data.frame(Date=dates,Data=data,groupID=groupID)
BB=c(12,12,16,24,35,35)
DF[DF$groupID %in% BB,]
Date Data groupID
1 2015-01-01 4.4104202 12
3 2015-01-03 2.1557735 16
4 2015-01-04 -0.9880946 24
5 2015-01-05 -0.3396025 35
我需要根据向量 BB
中与 groupID 列匹配的值过滤数据框 DF
。但是,如果 BB
包含重复项,则不会反映在结果中。
由于我的向量 BB
包含两个值 1 和两个值 5,因此输出实际上应该是:
Date Data groupID
1 2015-01-01 4.4104202 12
1 2015-01-01 4.4104202 12
3 2015-01-03 2.1557735 16
4 2015-01-04 -0.9880946 24
5 2015-01-05 -0.3396025 35
5 2015-01-05 -0.3396025 35
有办法实现吗?并尽可能保持向量的顺序 BB
?
使用match()
(or findInterval()
):
DF[match(BB,DF$groupID),];
## Date Data groupID
## 1 2015-01-01 1.2199835 12
## 1.1 2015-01-01 1.2199835 12
## 3 2015-01-03 1.8141556 16
## 4 2015-01-04 0.2748579 24
## 5 2015-01-05 3.2030200 35
## 5.1 2015-01-05 3.2030200 35
(注意Data
栏是不同的,因为你使用rnorm()
生成它没有先调用set.seed()
。建议在任何代码中调用set.seed()
将随机性纳入其中的示例,以便可以重现准确的结果。)
你可以把BB
变成一个data.frame
,然后用merge()
把DF
和BB
按照它们的groupID
合并,就可以了具体点:
dates=seq(as.POSIXct("2015-01-01 00:00:00"), as.POSIXct("2015-01-07 00:00:00"), by="day")
groupID=c(12,14,16,24,35,46,54)
set.seed(1234)
data=rnorm(7,1,2)
DF=data.frame(Date=dates,Data=data,groupID=groupID)
BB=data.frame(groupID=c(12,12,16,24,35,35))
测试结果:
>merge(DF,BB,by="groupID")
groupID Date Data
1 12 2015-01-01 -1.414131
2 12 2015-01-01 -1.414131
3 16 2015-01-03 3.168882
4 24 2015-01-04 -3.691395
5 35 2015-01-05 1.858249
6 35 2015-01-05 1.858249