简体   繁体   中英

refer to column name from variable in across in dplyr

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 (eg 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)))))

您还可以使用get()

m %>% mutate(across(.fns = list(~ log(.) - log(get(denom)))))

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

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)) )))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM