I see gobs of posts about passing column names as strings to function but none of them consider this use case. All the methods I see don't work. Here is one. Please compare the what_I_want column to the what_I_get column below. I want the value of items in the column, not the column name, of course. Thanks.
library(dplyr)
Fun <- function(df,column) {
df %>%
mutate(what_I_want = cyl) %>%
# current best practice? Doen't work in this case.
mutate(what_I_get := {{column}})
}
mtcars[1:2,1:3] %>% Fun("cyl")
#> mpg cyl disp what_I_want what_I_get
#> Mazda RX4 21 6 160 6 cyl
#> Mazda RX4 Wag 21 6 160 6 cyl
Created on 2022-11-07 with reprex v2.0.2
Just add get
Fun <- function(df,column) {
df %>%
mutate(what_I_want = get(column) )
}
mtcars[1:2,1:3] %>% Fun("cyl")
mpg cyl disp what_I_want
Mazda RX4 21 6 160 6
Mazda RX4 Wag 21 6 160 6
We may use ensym which can take both quoted as well as unquoted column name
Fun <- function(df,column) {
df %>%
mutate(what_I_want = !! rlang::ensym(column))
}
-testing
> mtcars[1:2,1:3] %>% Fun("cyl")
mpg cyl disp what_I_want
Mazda RX4 21 6 160 6
Mazda RX4 Wag 21 6 160 6
> mtcars[1:2,1:3] %>% Fun(cyl)
mpg cyl disp what_I_want
Mazda RX4 21 6 160 6
Mazda RX4 Wag 21 6 160 6
Using the .data pronoun you could do:
library(dplyr)
Fun <- function(df,column) {
df %>%
mutate(what_I_get = .data[[column]])
}
mtcars[1:2,1:3] %>%
Fun("cyl")
#> mpg cyl disp what_I_get
#> Mazda RX4 21 6 160 6
#> Mazda RX4 Wag 21 6 160 6
For more on the .data pronoun see Data mask programming patterns.
Related
I want to rename a variable in my dataframe using dplyr to have spaces but this variable name is a concatenation of a dynamic variable and a static string. In the following example, I'd need "Test1" to be a dynamic variable
df <- mtcars %>% select(`Test1 mpg` = "mpg")
So when I try this, I end up with an error:
var <- "Test1"
df <- mtcars %>% select(paste0(var, " mpg") = "mpg")
How could I go about making those new variable names dynamic?
Using the special assignment operator := you could do:
library(dplyr)
df <- mtcars %>% select(`Test1 mpg` = "mpg")
var <- "Test1"
mtcars %>%
select("{var} mpg" := "mpg")
#> Test1 mpg
#> Mazda RX4 21.0
#> Mazda RX4 Wag 21.0
#> Datsun 710 22.8
#> Hornet 4 Drive 21.4
or using !!sym():
mtcars %>%
select(!!sym(paste(var, " mpg")) := "mpg")
#> Test1 mpg
#> Mazda RX4 21.0
#> Mazda RX4 Wag 21.0
#> Datsun 710 22.8
#> Hornet 4 Drive 21.4
Take this toy function - just a wrapper around dplyr::select:
select_col <- function(df, chosen_col) {
if(is.character(enquo(chosen_col))){
df %>%
select(all_of(chosen_col))
} else {
df %>%
select({{ chosen_col }})
}
}
What it does is allows the selection of a column passed to chosen_col, regardless of whether that column is presented as a data variable masked as an environmental variable or a string masked as an environmental variable.
Both of these return the same thing:
mtcars %>%
select_col(chosen_col = mpg) %>%
head(2)
mpg
Mazda RX4 21
Mazda RX4 Wag 21
mtcars %>%
select_col(chosen_col = "mpg") %>%
head(2)
mpg
Mazda RX4 21
Mazda RX4 Wag 21
While select_col works, what I want is something more like this doesn't work:
select_col_desired <- function(df, chosen_col) {
if(is.character(enquo(chosen_col))){
chosen_col <- convert_to_env_variable(chosen_col)
}
df %>%
select({{ chosen_col }})
}
What can I use in place of the non-existent function convert_to_env_variablesuch that select_col_desired returns the same things as select_col?
I am aware that select does this already outside a function.
Solution 1: Single Column
If you truly want something like convert_to_env_variable(), you can just use rlang::enquo() to begin with, and skip the conditional.
select_col_desired <- function(df, chosen_col) {
chosen_col <- enquo(chosen_col)
df %>%
select(!!chosen_col)
}
Heck, you could just skip the enquo() and !! altogether:
select_col_desired <- function(df, chosen_col) {
df %>%
select({{ chosen_col }})
}
This is because the "curly-curly" operator {{ first captures the argument passed to chosen_col and immediately defuses it as an expression; before substituting the result in its own place, here to be evaluated as a data-variable in the context of df.
Result
Either way, you'll get this:
# With column as unquoted symbol.
mtcars %>%
select_col_desired(chosen_col = mpg) %>%
head(2)
#> mpg
#> Mazda RX4 21
#> Mazda RX4 Wag 21
# With column as string.
mtcars %>%
select_col_desired(chosen_col = "mpg") %>%
head(2)
#> mpg
#> Mazda RX4 21
#> Mazda RX4 Wag 21
Solution 2: Arbitrary Selection
For a more general solution, you could simply pass your columns as ... to select():
select_any_desired <- function(df, ...) {
df %>% select(...)
}
Result
# With column as unquoted symbol.
mtcars %>%
select_col_desired(mpg) %>%
head(2)
#> mpg
#> Mazda RX4 21
#> Mazda RX4 Wag 21
# With column as string.
mtcars %>%
select_col_desired("mpg") %>%
head(2)
#> mpg
#> Mazda RX4 21
#> Mazda RX4 Wag 21
Note
If you're writing a package, you'll naturally need to #import or qualify functions like rlang::enquo() and magrittr:`%>%`() and dplyr::select(); rather than using library().
Step 1: get the name for the variable
This is done via as.character(substitute(chosen_col)) and we also want to save it for step 2.
varname <- as.character(substitute(chosen_col))
Step 2: assign to a different environment
Assignment can be done using assign(x, value, envir=parent.frame()). Here it is essential to use parent.frame() as assigning environment since we want to assign outside of the function and default behavior is to assign to current environment. Another option here would be to assign to the global environment with envir=globalenv.
This leads us to
assign(varname, x, envir=parent.frame())
where x is containing everything we want to assign. Read this as varname <- x.
Putting both steps
select_col <- function(df, chosen_col) {
if(is.character(enquo(chosen_col))){
x <- df %>%
select(all_of(chosen_col))
} else {
x <- df %>%
select({{ chosen_col }})
}
varname <- as.character(substitute(chosen_col))
assign(varname, x, envir=parent.frame())
x
}
select_col(mtcars, "mpg") %>% head(2)
#> mpg
#> Mazda RX4 21
#> Mazda RX4 Wag 21
head(mpg, 2)
#> mpg
#> Mazda RX4 21
#> Mazda RX4 Wag 21
rm(mpg)
select_col(mtcars, mpg) %>% head(2)
#> mpg
#> Mazda RX4 21
#> Mazda RX4 Wag 21
head(mpg, 2)
#> mpg
#> Mazda RX4 21
#> Mazda RX4 Wag 21
Is it possible to use the pipe Operator in R (not to get) but to set data?
Lets say i want to modify the first row of mtcars dataset and set the value of qsec to 99.
Traditional way:
mtcars[1, 7] <- 99
Is that also possible using the pipe Operator?
mtcars %>% filter(qsec == 16.46) %>% select(qsec) <- 99
If we are in a state where the chain is absolute necessary or curious to know whether <- can be applied in a chain
library(magrittr)
mtcars %>%
`[<-`(1, 7, 99) %>%
head(2)
# mpg cyl disp hp drat wt qsec vs am gear carb
#Mazda RX4 21 6 160 110 3.9 2.620 99.00 0 1 4 4
#Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Also, inset (from the comments) is an alias for [<-
mtcars %>%
inset(1, 7, 99) %>%
head(2)
I'm trying to dynamically rename a single column in a dataframe using rename() with the tidy evaluation syntax released in dplyr v0.7.0.
From the ?rename help page, I found the following example to rename 2 columns.
library(dplyr)
vars <- c(var1 = "cyl", var2 ="am")
rename(mtcars, !!vars) %>% head(1)
> mpg var1 disp hp drat wt qsec vs var2 gear carb
> Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
However, I've noticed I cannot use this same syntax to rename a single column.
vars <- c(var1 = "cyl")
rename(mtcars, !!vars) %>% head(1)
> Error: All arguments must be named
Yet, when I rename the same column twice, it works.
vars <- c(var1 = "cyl", var1 = "cyl")
rename(mtcars, !!vars) %>% head(1)
> mpg var1 disp hp drat wt qsec vs am gear carb
> Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
Why is this happening? What is the correct syntax?
Use the !!! for evaluation
rename(mtcars, !!!vars) %>%
head(1)
# mpg var1 disp hp drat wt qsec vs am gear carb
#Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
I have been using this formula:
varlist <- c("A", "B")
for(i in c(1:2)) {
print(varlist[i])
print(summary(svyglm(as.formula(paste0(varlist[i], "~YEAR + REGION")),
design = subset(FEI.w, varlist[i] != "U"),
family = quasibinomial)))
}
I have more variables than just A and B, but I want to do a glm in the survey package using A and B as my dependent variable.
The problem I am running into is that when I subset the data to exclude unknown values in A and B, R doesn't do it and includes the whole data frame.
Any pointers as to why this is happening and how to fix this would be very much appreciated!
subset() uses non-standard evaluation, which means it takes the column names as unquoted variables, e.g.
subset(mtcars, mpg == 21)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
vs
subset(mtcars, "mpg" == 21)
#> [1] mpg cyl disp hp drat wt qsec vs am gear carb
#> <0 rows> (or 0-length row.names)
Your varlist[i] != "U" compares the literal strings "A" and "U" and finds that they aren't equal.
You might be able to get around this with
eval(parse(text = varlist[i])) != "U"
i.e.
subset(mtcars, eval(parse(text="mpg")) == 21)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
but the old adage goes that if you're using eval(parse( then something has probably gone wrong.
svyglm has a subset parameter so you don't need to call subset on the design object. You should do the subsetting like this:
library(survey)
data(api)
dstrat<-svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc)
rstrat<-as.svrepdesign(dstrat)
for (type in unique(apistrat$stype)) {
print(summary(svyglm(api00~ell+meals+mobility,
design = rstrat,
subset = apistrat$stype==type)))
}