12.7 C
London
Monday, June 2, 2025
HomeTidyverse in Rdplyr in RHow to Pass a String as Variable Name in dplyr

How to Pass a String as Variable Name in dplyr

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories