您如何做条件“左联接"?在R中? [英] How do you do conditional "left join" in R?

查看:70
本文介绍了您如何做条件“左联接"?在R中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己在R中多次执行条件左连接".如果您有两个数据框,例如:

I have found myself doing a "conditional left join" several times in R. To illustrate with an example; if you have two data frames such as:

> df
    a b
  1 1 0
  2 2 0

> other.df
    a b
  1 2 3

目标是最终得到以下数据框:

The goal is to end up with this data frame:

> final.df
    a b
  1 1 0
  2 2 3

到目前为止我已经编写的代码:

The code I've been written so far:

c <- merge(df, other.df, by=c("a"), all.x = TRUE)
c[is.na(c$b.y),]$b.y <- 0
d<-subset(c, select=c("a","b.y"))
colnames(d)[2]<-b

最终得到我想要的结果.

to finally arrive with the result I wanted.

以四行有效地执行此操作会使代码非常不透明. 有没有更好,更省事的方法呢?

Doing this in effectively four lines makes the code very opaque. Is there any better, less cumbersome way to do this?

推荐答案

这里有两种方法.在这两种情况下,第一行都进行左合并,返回所需的列.在merge的情况下,我们必须设置名称.两行的最后一行将NA s替换为0.

Here are two ways. In both cases the first line does a left merge returning the required columns. In the case of merge we then have to set the names. The final line in both lines replaces NAs with 0.

合并

res1 <- merge(df, other.df, by = "a", all.x = TRUE)[-2]
names(res1) <- names(df)
res1[is.na(res1)] <- 0

sqldf

library(sqldf)
res2 <- sqldf("select a, o.b from df left join 'other.df' o using(a)")
res2[is.na(res2)] <- 0

这篇关于您如何做条件“左联接"?在R中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆