将数据集中的行逐行匹配到另一个数据框中的变量列表

时间:2016-02-11 14:47:22

标签: r statistics

我有一个包含国家/地区列表的数据集“ds1”,以及包含其他国家/地区列表的数据框“df1”

问题是将“ds1”中的变量逐行与数据框“df1”中的变量列表进行匹配,并得到一个包含结果的列。

理想的输出是:

df1 <- c("BE","BG","CZ","DK","DE","EE","IE","EL","ES","FR","HR","IT","CY","LV","LT","LU","HU","MT","NL","AT","PL","PT","RO","SI","SK","FI","SE","UK")
ds1 <- data.frame(c.id = c("US","BG","CZ","DK","CA","EE","IE","NZ","GR","FR","HR","IT"))

> ds1
   c.id
1    US
2    BG
3    CZ
4    DK
5    CA
6    EE
7    IE
8    NZ
9    GR
10   FR
11   HR
12   IT


> df1
 [1] "BE" "BG" "CZ" "DK" "DE" "EE" "IE" "EL" "ES" "FR" "HR" "IT" "CY" "LV" "LT" "LU" "HU" "MT" "NL" "AT" "PL" "PT" "RO" "SI" "SK" "FI" "SE" "UK"

和理想的输出可能是:

> ds1
   c.id   match
1    US   FALSE
2    BG   TRUE
3    CZ   TRUE
4    DK   TRUE
5    CA   FALSE
6    EE   TRUE
7    IE   TRUE
8    NZ   FALSE
9    GR   TRUE
10   FR   TRUE
11   HR   TRUE
12   IT   TRUE

由于

1 个答案:

答案 0 :(得分:3)

ds1$match <- ds1$c.id %in% df1
ds1