按字符列名称过滤数据(在dplyr中) [英] Filter data frame by character column name (in dplyr)

查看:179
本文介绍了按字符列名称过滤数据(在dplyr中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框架,并希望以两种方式之一进行过滤,通过这个列或那个列。我希望能够将列名作为变量引用。如何(在 dplyr ,如果这有所作为)我是否通过变量引用列名?

I have a data frame and want to filter it in one of two ways, by either column "this" or column "that". I would like to be able to refer to the column name as a variable. How (in dplyr, if that makes a difference) do I refer to a column name by a variable?

library(dplyr)
df <- data.frame(this = c(1, 2, 2), that = c(1, 1, 2))
df
#   this that
# 1    1    1
# 2    2    1
# 3    2    2
df %>% filter(this == 1)
#   this that
# 1    1    1

但是说我想使用变量保存this或that,并对的值进行过滤。 as.symbol get 在其他上下文中工作,但不是这样:

But say I want to use the variable column to hold either "this" or "that", and filter on whatever the value of column is. Both as.symbol and get work in other contexts, but not this:

column <- "this"
df %>% filter(as.symbol(column) == 1)
# [1] this that
# <0 rows> (or 0-length row.names)
df %>% filter(get(column) == 1)
# Error in get("this") : object 'this' not found

如何将的值转换为列名称?

How can I turn the value of column into a column name?

推荐答案

我会避开使用 get()全部一起。在这种情况下,这似乎是相当危险的,尤其是在编程中。您可以使用未经评估的电话或粘贴的字符串,但您需要使用 filter _()而不是 filter()

I would steer clear of using get() all together. It seems like it would be quite dangerous in this situation, especially if you're programming. You could use either an unevaluated call or a pasted character string, but you'll need to use filter_() instead of filter().

df <- data.frame(this = c(1, 2, 2), that = c(1, 1, 2))
column <- "this"

strong>选项1 - 使用未经评估的电话:

Option 1 - using an unevaluated call:

您可以将 y code> 1 ,但这里我将其显示为 y 来说明如何轻松更改表达式值。

You can hard-code y as 1, but here I show it as y to illustrate how you can change the expression values easily.

expr <- lazyeval::interp(quote(x == y), x = as.name(column), y = 1)
## or 
## expr <- substitute(x == y, list(x = as.name(column), y = 1))
df %>% filter_(expr)
#   this that
# 1    1    1

选项2 - 使用 paste()(显然更容易):

Option 2 - using paste() (and obviously easier):

df %>% filter_(paste(column, "==", 1))
#   this that
# 1    1    1

这两个选项的主要内容是我们需要使用 filter _()而不是 filter()。实际上,从我所看到的,如果你用 dplyr 编程,你应该总是使用 * _()函数。

The main thing about these two options is that we need to use filter_() instead of filter(). In fact, from what I've read, if you're programming with dplyr you should always use the *_() functions.

我使用这篇文章作为有用的参考:字符串作为函数参数r ,我使用 dplyr 版本0.3.0.2。

I used this post as a helpful reference: character string as function argument r, and I'm using dplyr version 0.3.0.2.

这篇关于按字符列名称过滤数据(在dplyr中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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