Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triple exclamation marks on R

Tags:

r

I've been reading a book on feature engineering, and a piece of code has a triple exclamation mark which I don't understand:

vc_pred <- 
  recipe(Stroke ~ ., data = stroke_train %>% dplyr::select(Stroke, !!!VC_preds)) %>% 
  step_YeoJohnson(all_predictors()) %>% 
  prep(stroke_train %>% dplyr::select(Stroke, !!!VC_preds)) %>% 
  juice() %>% 
  gather(Predictor, value, -Stroke)

VC_preds is a vector containing the variable names of continuous predictors. I understand all the code except by the !!! mark. One ! is supposed to be a negation, but what does it mean !!!?

Any help provided will be greatly appreciated. Thank you.

Regards,

Alexis

like image 450
Alexis Avatar asked Apr 13 '20 01:04

Alexis


People also ask

What does triple exclamation mark mean in R?

!!! is usually used to evaluate a list of expressions. library(dplyr) library(rlang) VC_preds <- c('mpg', 'cyl') mtcars %>% select(!!!

What do double exclamation marks mean in R?

The double exclamation mark is just syntactic sugar for that phrase. pull , also recently added to dplyr, pulls out a vector (column) out of a data frame.

What is the use of exclamation mark in R?

Common operators The exclamation point ( ! ) allows us to a perform a NOT check, by negating or swapping a check's result. This allows you say things like “is this check true and that check not true?”.

What do multiple exclamation marks mean?

It is used to end a rhetorical question or a simultaneous question and exclamation. Some writers, then, began using multiple exclamation points as a logical outgrowth of the interbang and single exclamation mark to add even more emphasis to words, phrases, and sentences.


1 Answers

!!! is usually used to evaluate a list of expressions.

library(dplyr)
library(rlang)

VC_preds <- c('mpg', 'cyl')
mtcars %>% select(!!!VC_preds) %>% head

#                   mpg Cyl
#Mazda RX4         21.0   6
#Mazda RX4 Wag     21.0   6
#Datsun 710        22.8   4
#Hornet 4 Drive    21.4   6
#Hornet Sportabout 18.7   8
#Valiant           18.1   6

If VC_preds is a vector as in your example, !! should work as well.

mtcars %>% select(!!VC_preds) %>% head

Help page of ?"!!!" gives a better example to understand the difference.

vars <- syms(c("height", "mass"))
vars
#[[1]]
#height

#[[2]]
#mass

starwars %>% select(!!!vars)
# A tibble: 87 x 2
#   height  mass
#    <int> <dbl>
# 1    172    77
# 2    167    75
# 3     96    32
# 4    202   136
# 5    150    49
# 6    178   120
# 7    165    75
# 8     97    32
# 9    183    84
#10    182    77
# … with 77 more rows
like image 85
Ronak Shah Avatar answered Oct 17 '22 22:10

Ronak Shah