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

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

Avoid duplicates columns with left join sqldf

问题

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

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

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

谢谢

Chloé

英文:

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

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

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

  1. library(sqldf)
  2. # test inputs
  3. table1 &lt;- data.frame(id = 0, a = 3)
  4. table2 &lt;- data.frame(id = 0, var1 = 1, var2 = 2)
  5. sqldf(
  6. &quot; SELECT a.*, b.var1, b.var2
  7. FROM table1 as a LEFT JOIN table2 as b
  8. ON a.id = b.id &quot;
  9. )
  10. ## id a var1 var2
  11. ## 1 0 3 1 2

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

  1. sqldf(
  2. &quot; SELECT *
  3. FROM table1 as a LEFT JOIN table2 as b
  4. USING(id) &quot;
  5. )
  6. ## id a var1 var2
  7. ## 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.

  1. sqldf(
  2. &quot; SELECT a.*, b.var1, b.var2, b.id as id2
  3. FROM table1 as a LEFT JOIN table2 as b
  4. ON a.id=b.id &quot;
  5. )
  6. ## id a var1 var2 id2
  7. ## 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:

确定