Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如果两列的组合正好匹配其他两列,则子集数据帧_R - Fatal编程技术网

R 如果两列的组合正好匹配其他两列,则子集数据帧

R 如果两列的组合正好匹配其他两列,则子集数据帧,r,R,我有类似的数据 x1 <- data.frame(state = c("FL","FL","TX","TX"), county = c("Duval","Columbia","Dallam","Dimmit")) x2 <- data.frame(state = c("FL","FL","FL","TX","TX","TX"), county = c("Duval","Columbia","Pinellas","Dallam","Dimmit","Duval"), UR = c(4,

我有类似的数据

x1 <- data.frame(state = c("FL","FL","TX","TX"), county = c("Duval","Columbia","Dallam","Dimmit"))
x2 <- data.frame(state = c("FL","FL","FL","TX","TX","TX"), county = c("Duval","Columbia","Pinellas","Dallam","Dimmit","Duval"), UR = c(4,5,7,4,6,3))

x3 <- subset(x2, county %in% x1$county & state %in% x1$state)    

x1您要查找的是左连接:

> library(dplyr)
> left_join(x1, x2, by = c('state', 'county'))
  state   county UR
1    FL    Duval  4
2    FL Columbia  5
3    TX   Dallam  4
4    TX   Dimmit  6
或使用基本R中的
合并

> merge(x1, x2, all.x = T)
  state   county UR
1    FL Columbia  5
2    FL    Duval  4
3    TX   Dallam  4
4    TX   Dimmit  6

使用
数据表

library(data.table)
setDT(x2)[x1, on = .(state, county)]
#   state   county UR
#1:    FL    Duval  4
#2:    FL Columbia  5
#3:    TX   Dallam  4
#4:    TX   Dimmit  6

尝试查找:
?合并
。很好,就是这样。谢谢您!