dplyr:data.frame中的两列相等的过滤器 [英] dplyr: filter where two columns in data.frame are equal

查看:523
本文介绍了dplyr:data.frame中的两列相等的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在基数R中,可以轻松地过滤到两列如下所示的行:

In base R one can easily filter to rows where two columns are equals like so:

mtcars[mtcars$cyl==mtcars$carb,]

使用 dplyr 过滤器这可以很容易地完成

Using dplyr's filter this can be done easily

mtcars %>% filter(cyl==carb)

但是,如果我使用这段代码编写一个函数,我想使用 filter _ ,但此代码无效

But if I am writing a function using this code I would want to use filter_, but this code doesn't work

mtcars %>% filter_("cyl"=="carb")

由于在这种情况下,它认为 是一个值而不是一个变量。

Since in this case it thinks "carb" is a value to test rather than a variable.

我的问题是如何使用 filter _ 来比较data.frame中的两个变量?

My question is how can you use filter_ to compare two variables in a data.frame?

推荐答案

将整个事情引入引号:

mtcars %>% filter_("cyl==carb")

或者,已经建议,这也将工作:

Or, as effel has already suggested, this will also work:

mtcars %>% filter_(~cyl==carb)

这篇关于dplyr:data.frame中的两列相等的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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