In Stata, any part of the code can be substituted with macros. There are no macros in R: you cannot directly substitute some part of your code by enclosing it in special quotes. You can only substitute objects in R
You can replace scalars:
Stata | scalar x = 10 keep if _n <= `x' |
dplyr | x <- 10 slice(df, 1:x) |
You can replace strings:
Stata | local x MYFILE import delimited ~/`x'.csv |
dplyr | x <- "MYFILE" read_csv(paste("~/", x, ".csv", sep = "")) |
You can replace functions:
Stata | local x mean egen v1_mean = `x'(v1), by(id) |
dplyr | x <- mean df %>% group_by(id) %>% mutate(v1_mean = x(v1)) |
You can replace variable names
To substitute one variable (given as a symbol), use {{}}
var <- sym("height")
starwars %>% select({{var}})
starwars %>% mutate("mean_{{var}}" := mean({{var}}))
To substitute several variables (given as a vector of characters), use all_of()
vars <- c("height", "birth_year")
starwars %>% select(all_of(vars))
starwars %>% mutate(across(all_of(vars), mean))