我想基于另一个数据框中包含的多个标识变量从数据框中提取选择的行。请考虑以下说明性数据集:
df <- data.frame(id=c(1,2,2,3,4,4,4,4,5), ref=c("A","B","C","D","E","F","F","G","H"), amount=c(10,15,20,25,30,35,-35,40,45))
required <- data.frame(id=c(2,3,4,4), ref=c("B","D","E","F"))
我想在id,ref和amount的数据框中输出如下:
id ref amount
2 B 15
3 D 25
4 E 30
4 F 35
4 F -35
请注意,id 4和ref F有两个来自df的匹配,金额为35和-35。
答案 0 :(得分:1)
您想要合并:
merge(df, required)
## id ref amount
## 1 2 B 15
## 2 3 D 25
## 3 4 E 30
## 4 4 F 35
## 5 4 F -35