简体   繁体   English

在R中按ID左联接变量

[英]left join a variable by id in R

I have two tables df1 and df2. 我有两个表df1和df2。 Now I want to left join a variable newVar of df2 to df1 by the same id. 现在,我想通过相同的ID将df2的变量newVar加入df1。 But the problem is that there are some ids exist in df1 but not in df2. 但是问题是df1中存在一些ID,而df2中不存在。

So I want that if df1.id exists in df2, then df1.newVar=df2.newVar, else df1.newVar=0 . 所以我想if df1.id exists in df2, then df1.newVar=df2.newVar, else df1.newVar=0

In R, I've tried df1$newVar= ifelse((df1$id %in% df2$id), df2$newVar, 0) but the result was not good. 在R中,我尝试了df1$newVar= ifelse((df1$id %in% df2$id), df2$newVar, 0)但结果不好。

I've tried also sqldf('select df1.*, df2.newVar as newVar from df1 left outer join df2 on df2.id= df1.id') but R gives an error. 我也尝试了sqldf('select df1.*, df2.newVar as newVar from df1 left outer join df2 on df2.id= df1.id')但是R给出了错误。

Here is an example of df1: 这是df1的示例:

id   var1
A     1
A     2
B     1

df2: df2:

id   newVar
A     1
A     1

I want the result of df1 looks like: 我希望df1的结果如下所示:

id   var1   newVar
A     1       1
A     2       1
B     1       0

You can use left_join{dplyr} to do that work. 您可以使用left_join {dplyr}来完成这项工作。

library("dplyr")
df1 <- data_frame(
  id = c("A", "A", "B"),
  var1 = c(1, 2, 1)
)

df2 <- data_frame(
  id = c("A", "A"),
  newvar = c(1, 1)
)

unique(left_join(df1,df2,by="id"))

And the result is : 结果是:

# A tibble: 3 × 3
     id  var1 newvar
  <chr> <dbl>  <dbl>
1     A     1      1
2     A     2      1
3     B     1     NA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM