避免在左连接中出现重复列,使用sqldf。

huangapple go评论58阅读模式
英文:

Avoid duplicates columns with left join sqldf

问题

我尝试在R中使用sqldf包进行左连接。

data <- sqldf(
"SELECT a.*, b.var1, b.var2, b.id 
FROM table1 as a LEFT JOIN table2 as b 
ON a.id=b.id "
)

不幸的是,我在data中得到了两列名为"id"的列。我该如何避免这个问题?

谢谢

Chloé

英文:

I'm trying to do a left join using sqldf package on R.

data &lt;- sqldf(
&quot; SELECT a.*, b.var1, b.var2, b.id 
FROM table1 as a LEFT JOIN table2 as b 
ON a.id=b.id &quot;
)

Unfortunately I get two columns named "id" in data. How can I avoid this problem?

Thanks

Chloé

答案1

得分: 1

以下是代码的翻译部分:

请注意,代码要求两个id,a.* 包括所有 a 列,同时明确要求 b.id。你可以简单地去掉 b.id

或者,如果你想要所有 b 列,除了id之外,可以使用 USING 而不是 ON

如果你确实想要两个id,只是命名不同,那么这将把 b.id 命名为 id2

英文:

Note that the code is asking for two id's as a.* includes all a columns and it explicitly asked for b.id. You can simply drop the b.id

library(sqldf)
# test inputs
table1 &lt;- data.frame(id = 0, a = 3)
table2 &lt;- data.frame(id = 0, var1 = 1, var2 = 2)

sqldf(
  &quot; SELECT a.*, b.var1, b.var2
    FROM table1 as a LEFT JOIN table2 as b 
    ON a.id = b.id &quot;
)
##   id a var1 var2
## 1  0 3    1    2

or if you want all b columns except id then use USING instead of ON

sqldf(
  &quot; SELECT *
    FROM table1 as a LEFT JOIN table2 as b 
    USING(id) &quot;
)
##   id a var1 var2
## 1  0 3    1    2

If you do want two id's but just named differently then this will name the b.id as id2.

sqldf(
  &quot; SELECT a.*, b.var1, b.var2, b.id as id2
  FROM table1 as a LEFT JOIN table2 as b 
  ON a.id=b.id &quot;
)
##   id a var1 var2 id2
## 1  0 3    1    2   0

huangapple
  • 本文由 发表于 2023年5月25日 20:27:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332269.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定