Reputation: 4104
Given a reference column z
, I want to use dplyr
to transform each column as:
x = log(x) - log(z)
I want z
to be a string, or even better, a quoted expression (e.g. input by a user - all of this is within a function).
Here is what I've tried:
library(dplyr)
m <- data.frame(x=1:5,y=11:15,z=21:25)
denom = "z"
This works:
m %>%
mutate(across(x:z ,
list(~ log(.) - log(z) )))
This fails:
m %>%
mutate(across(x:z ,
list(~ log(.) - log(rlang::sym(denom)))))
# Error: Problem with `mutate()` input `..1`.
# ℹ `..1 = across(x:z, list(~log(.) - log(rlang::sym(denom))))`.
# ✖ non-numeric argument to mathematical function
# Run `rlang::last_error()` to see where the error occurred.
This also fails:
m %>%
mutate(across(x:z ,
list(~ log(.) - log(!!denom) )))
# Error: Problem with `mutate()` input `..1`.
# ℹ `..1 = across(x:z, list(~log(.) - log("z")))`.
# ✖ non-numeric argument to mathematical function
# Run `rlang::last_error()` to see where the error occurred.
# > #list(~ log(.) - log(rlang::sym(denom)))))
Upvotes: 3
Views: 1657
Reputation: 39657
In case there is a more complex situation than simple selecting one column eval
with str2lang
(or parse
) could be used. In case it is an expression it could be used direct in eval
denom <- "z"
m %>% mutate(across(x:z, list(~ log(.) - log(eval(str2lang(denom))) )))
# x y z x_1 y_1 z_1
#1 1 11 21 -3.044522 -0.6466272 0
#2 2 12 22 -2.397895 -0.6061358 0
#3 3 13 23 -2.036882 -0.5705449 0
#4 4 14 24 -1.791759 -0.5389965 0
#5 5 15 25 -1.609438 -0.5108256 0
denom <- expression(z)
m %>% mutate(across(x:z, list(~ log(.) - log(eval(denom)) )))
# x y z x_1 y_1 z_1
#1 1 11 21 -3.044522 -0.6466272 0
#2 2 12 22 -2.397895 -0.6061358 0
#3 3 13 23 -2.036882 -0.5705449 0
#4 4 14 24 -1.791759 -0.5389965 0
#5 5 15 25 -1.609438 -0.5108256 0
m %>% mutate(across(x:z, list(~ log(.) - log(z) )))
# x y z x_1 y_1 z_1
#1 1 11 21 -3.044522 -0.6466272 0
#2 2 12 22 -2.397895 -0.6061358 0
#3 3 13 23 -2.036882 -0.5705449 0
#4 4 14 24 -1.791759 -0.5389965 0
#5 5 15 25 -1.609438 -0.5108256 0
More complex:
denom <- "x + y"
m %>% mutate(across(x:z, list(~ log(.) - log(eval(str2lang(denom))) )))
# x y z x_1 y_1 z_1
#1 1 11 21 -2.484907 -0.08701138 0.5596158
#2 2 12 22 -1.945910 -0.15415068 0.4519851
#3 3 13 23 -1.673976 -0.20763936 0.3629055
#4 4 14 24 -1.504077 -0.25131443 0.2876821
#5 5 15 25 -1.386294 -0.28768207 0.2231436
denom <- expression(x + y)
m %>% mutate(across(x:z, list(~ log(.) - log(eval(denom)) )))
# x y z x_1 y_1 z_1
#1 1 11 21 -2.484907 -0.08701138 0.5596158
#2 2 12 22 -1.945910 -0.15415068 0.4519851
#3 3 13 23 -1.673976 -0.20763936 0.3629055
#4 4 14 24 -1.504077 -0.25131443 0.2876821
#5 5 15 25 -1.386294 -0.28768207 0.2231436
m %>% mutate(across(x:z, list(~ log(.) - log(x + y) )))
# x y z x_1 y_1 z_1
#1 1 11 21 -2.484907 -0.08701138 0.5596158
#2 2 12 22 -1.945910 -0.15415068 0.4519851
#3 3 13 23 -1.673976 -0.20763936 0.3629055
#4 4 14 24 -1.504077 -0.25131443 0.2876821
#5 5 15 25 -1.386294 -0.28768207 0.2231436
Upvotes: 1
Reputation: 2217
You can also use get()
:
m %>% mutate(across(.fns = list(~ log(.) - log(get(denom)))))
Upvotes: 6
Reputation: 16978
I don't know, if this is a good way to code, but you can do
library(dplyr)
m %>%
mutate(across(x:z ,
list(~ log(.) - log(!!as.name(denom)) )))
Upvotes: 1
Reputation: 124083
Making use of the .data
pronoun from rlang
you could do:
library(dplyr)
m <- data.frame(x = 1:5, y = 11:15, z = 21:25)
denom <- "z"
m %>% mutate(across(
x:z,
list(~ log(.) - log(.data[[denom]]))
))
#> x y z x_1 y_1 z_1
#> 1 1 11 21 -3.044522 -0.6466272 0
#> 2 2 12 22 -2.397895 -0.6061358 0
#> 3 3 13 23 -2.036882 -0.5705449 0
#> 4 4 14 24 -1.791759 -0.5389965 0
#> 5 5 15 25 -1.609438 -0.5108256 0
Upvotes: 4