Horje
How do {{}} double curly brackets work in dplyr?

The use of double curly brackets {{}} is a feature introduced with the tidy evaluation and makes it easier to program with dplyr package. This article explains the theory behind double curly brackets and provides examples of how to use them effectively in R Programming Language.

Theory Behind {{}} in dplyr

The {{}} operator is known as the curly-curly operator or double curly brackets. It is part of the tidy evaluation (tidyeval) framework in R, which allows for more flexible and programmable data manipulation. Tidy evaluation helps in writing functions that can capture and manipulate expressions efficiently.

When programming with dplyr, it’s common to create functions that take column names as arguments. However, standard non-standard evaluation (NSE) in dplyr can make this challenging. The {{}} operator simplifies this by automatically handling the quoting and unquoting of expressions.

How It Works

  • Quasi-quotation: The {{}} operator performs quasi-quotation, which means it evaluates the expression inside it in the context of the data frame.
  • Handling column names: It allows you to pass column names as arguments to a function without worrying about quoting them.

Example 1: Summarizing Data

Suppose we want to create a function that summarizes a column by calculating the mean. Here’s how to do it with and without {{}}.

R
library(dplyr)

summarize_mean <- function(df, column) {
  df %>%
    summarise(mean_value = mean({{ column }}, na.rm = TRUE))
}

# Usage
summarize_mean(mtcars, mpg)

Output:

  mean_value
1 20.09062

In the above code, {{ column }} allows the function to interpret mpg as a column name in the mtcars dataset.

Example 2: Filtering Data

Let’s create a function that filters rows based on a condition applied to a specified column.

R
filter_data <- function(df, column, threshold) {
  df %>%
    filter({{ column }} > threshold)
}

# Usage
filter_data(mtcars, mpg, 20)

Output:

                mpg cyl  disp  hp drat    wt  qsec vs am gear carb mpg_transformed
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 1
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 1
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 1
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 1
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 1
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 1
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 1
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 1
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 1
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 1

Here, {{ column }} lets the function dynamically use the column name provided as an argument.

Example 3: Creating New Columns

We can also use {{}} to create functions that mutate or add new columns.

R
add_ratio <- function(df, numerator, denominator) {
  df %>%
    mutate(ratio = {{ numerator }} / {{ denominator }})
}

# Usage
add_ratio(mtcars, hp, wt)

Output:

                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb mpg_transformed
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 1.0000000
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 1.0000000
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 1.0000000
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 1.0000000

In this example, {{ numerator }} and {{ denominator }} allow the function to use the specified columns to create a new column ratio.

Conclusion

The double curly brackets {{}} in dplyr are a powerful tool for tidy evaluation, making it easier to write flexible and programmable functions for data manipulation. By understanding and using {{}}, you can simplify the handling of column names and expressions, leading to cleaner and more efficient R code. Whether you’re summarizing data, filtering rows, or creating new columns, the curly-curly operator enhances the capabilities of your dplyr functions.




Reffered: https://www.geeksforgeeks.org


R Language

Related
Function that calculates mean, variance, and skewness simultaneously in a dataframe in R Function that calculates mean, variance, and skewness simultaneously in a dataframe in R
How to make captions in ggplot2 more aesthetically pleasing? How to make captions in ggplot2 more aesthetically pleasing?
How to display mean with underline in base R plot? How to display mean with underline in base R plot?
Historydata Package in R Historydata Package in R
How to add trend line in a log-log plot (ggplot2)? How to add trend line in a log-log plot (ggplot2)?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14