Can a string be passed as a variable name in dplyr?

Can a string be passed as a variable name in dplyr?

No, a string cannot be passed as a variable name in dplyr. Dplyr requires the use of unquoted variable names, and passing a string would result in an error.

Pass a String as Variable Name in dplyr


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 <- data.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
<0 rows> (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.

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

Cite this article

stats writer (2024). Can a string be passed as a variable name in dplyr?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/can-a-string-be-passed-as-a-variable-name-in-dplyr/

stats writer. "Can a string be passed as a variable name in dplyr?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/can-a-string-be-passed-as-a-variable-name-in-dplyr/.

stats writer. "Can a string be passed as a variable name in dplyr?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/can-a-string-be-passed-as-a-variable-name-in-dplyr/.

stats writer (2024) 'Can a string be passed as a variable name in dplyr?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/can-a-string-be-passed-as-a-variable-name-in-dplyr/.

[1] stats writer, "Can a string be passed as a variable name in dplyr?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. Can a string be passed as a variable name in dplyr?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top