Home » How to Pass a String as Variable Name in dplyr

How to Pass a String as Variable Name in dplyr

by Tutor Aspire

You can use one of the following methods to pass a string as a variable name in dplyr:

Method 1: Use get()

df %>% filter(get(my_var) == 'A')

Method 2: Use .data

df %>% filter(.data[[my_var]] == 'A')

The following examples show how to use each method in practice with the following data frame:

#create data frame
df frame(team=c('A', 'A', 'A', 'B', 'B'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))

#view data frame
df

  team points assists rebounds
1    A     99      33       30
2    A     90      28       28
3    A     86      31       24
4    B     88      39       24
5    B     95      34       28

Example 1: Pass a String as Variable Name in dplyr Using get()

If we attempt to filter a data frame by passing a string as a variable name to the filter() function in dplyr, we’ll get an empty data frame as a result:

library(dplyr)

#define variable
my_var team'

#attempt to filter for rows where team is equal to a variable
df %>% filter(my_var == 'A')

[1] team     points   assists  rebounds
 (or 0-length row.names)

One way to get around this is to wrap the variable name in the get() function:

library(dplyr)

#define variable
my_var team'

#filter for rows where team is equal to a variable
df %>% filter(get(my_var) == 'A')

  team points assists rebounds
1    A     99      33       30
2    A     90      28       28
3    A     86      31       24

By using the get() function within the filter() function, we’re able to successfully filter the rows in the data frame for only the rows where team is equal to A.

Example 2: Pass a String as Variable Name in dplyr Using .data

Another way to pass a string as a variable name to the filter() function in dplyr is to use the .data function as follows:

library(dplyr)

#define variable
my_var team'

#filter for rows where team is equal to a variable
df %>% filter(.data[[my_var]] == 'A')

  team points assists rebounds
1    A     99      33       30
2    A     90      28       28
3    A     86      31       24

By using the .data function within the filter() function, we’re able to successfully filter the rows in the data frame for only the rows where team is equal to A.

Additional Resources

The following tutorials explain how to perform other common tasks in dplyr:

How to Select the First Row by Group Using dplyr
How to Filter by Multiple Conditions Using dplyr
How to Filter Rows that Contain a Certain String Using dplyr

You may also like