7

我有 18 对变量,我想对它们进行成对数学计算以计算 18 个新变量。将公式应用于一列时,dplyr 中的 cross() 函数非常方便。有没有办法将 cross() 应用于成对的列?

简单除法 2 个变量的小例子(我的实际代码会更复杂,一些 ifelse,...):

library(tidyverse)
library(glue)

# filler data
df <- data.frame("label" = c('a','b','c','d'),
                 "A" = c(4, 3, 8, 9),
                 "B" = c(10, 0, 4, 1),
                 "error_A" = c(0.4, 0.3, 0.2, 0.1),
                 "error_B" = c(0.3, 0, 0.4, 0.1))

# what I want to have in the end 
# instead of just 2 (A, B), I have 18
df1 <- df %>% mutate(
  'R_A' = A/error_A,
  'R_B' = B/error_B
)

# what I'm thinking about doing to use both variables A and error_A to calculate the new column
df2 <- df %>% mutate(
  across(c('A','B'),
         ~.x/{HOW DO I USE THE COLUMN WHOSE NAME IS glue('error_',.x)}
         .names = 'R_{.col}'
)
4

4 回答 4

8

一种选择是map/reduce。指定感兴趣的列('nm1'),在数据集中循环它们,map通过划分列绑定后的列(),并将这些与原始数据集绑定selectreducerename_dfc

library(dplyr)
library(purrr)
library(stringr)
nm1 <- c('A', 'B')
map_dfc(nm1, ~ df %>% 
                select(ends_with(.x)) %>% 
                reduce(., `/`) ) %>%
    rename_all(~ str_c('R_', nm1)) %>%
    bind_cols(df, .)

-输出

#  label A  B error_A error_B R_A      R_B
#1     a 4 10     0.4     0.3  10 33.33333
#2     b 3  0     0.3     0.0  10      NaN
#3     c 8  4     0.2     0.4  40 10.00000
#4     d 9  1     0.1     0.1  90 10.00000

或另一种选择across

df %>% 
    mutate(across(c(A, B), ~ 
     ./get(str_c('error_', cur_column() )), .names = 'R_{.col}' ))
#  label A  B error_A error_B R_A      R_B
#1     a 4 10     0.4     0.3  10 33.33333
#2     b 3  0     0.3     0.0  10      NaN
#3     c 8  4     0.2     0.4  40 10.00000
#4     d 9  1     0.1     0.1  90 10.00000    
于 2021-01-02T20:12:29.390 回答
4

一种选择可能是:

df %>%
 mutate(across(c(A, B), .names = "R_{col}")/across(starts_with("error")))

  label A  B error_A error_B R_A      R_B
1     a 4 10     0.4     0.3  10 33.33333
2     b 3  0     0.3     0.0  10      NaN
3     c 8  4     0.2     0.4  40 10.00000
4     d 9  1     0.1     0.1  90 10.00000
于 2021-01-02T21:45:42.067 回答
3

我喜欢上面的 akruns 回答,尤其是使用cur_column(). 有趣的是,cur_column()不能与 {rlang} 的评估 ( !! sym(paste0("error_", cur_column()))) 一起使用,但get它是一个很好的解决方法。

只是添加一种方法,它也适用于 dpylr < 1.0.0。我通常将mutate自定义函数与purrr::reduce(). 在这个函数x中是你的字符串词干,你构造了所有你想用!! sym(paste0(...)). 在左侧,您可以只使用 {rlang} 的粘合语法。

您可以通过调用reduce()字符串向量并data.frame输入.init = .参数来应用此自定义函数。

library(tidyverse)
library(glue)


# filler data
df <- data.frame("label" = c('a','b','c','d'),
                 "A" = c(4, 3, 8, 9),
                 "B" = c(10, 0, 4, 1),
                 "error_A" = c(0.4, 0.3, 0.2, 0.1),
                 "error_B" = c(0.3, 0, 0.4, 0.1))

gen_vars1 <- function(df, x) {
  
  mutate(df,
         "R_{x}" := !! sym(x) / !! sym(paste0("error_", x)))
}

df %>% 
  reduce(c("A", "B"), gen_vars1, .init = .)
#>   label A  B error_A error_B R_A      R_B
#> 1     a 4 10     0.4     0.3  10 33.33333
#> 2     b 3  0     0.3     0.0  10      NaN
#> 3     c 8  4     0.2     0.4  40 10.00000
#> 4     d 9  1     0.1     0.1  90 10.00000

reprex 包(v0.3.0)于 2021-01-02 创建

我曾经针对此类问题打开过功能请求,但显然它对于 {dplyr} 来说太特殊了。当您点击链接时,您还可以找到执行此类操作的另一个选项。

于 2021-01-02T21:06:24.603 回答
2

对于这种情况,我发现基本 R 解决方案也很简单有效。它不需要遍历列或唯一值。您定义两组列并直接划分它们。

对于您共享的示例,我们可以通过查找其中只有一个字符的列名称来识别列"A""B"

cols <- grep('^.$', names(df), value = TRUE)
error_cols <- grep('error', names(df), value = TRUE)

df[paste0('R_', cols)] <- df[cols]/df[error_cols]
df

#  label A  B error_A error_B R_A  R_B
#1     a 4 10     0.4     0.3  10 33.3
#2     b 3  0     0.3     0.0  10  NaN
#3     c 8  4     0.2     0.4  40 10.0
#4     d 9  1     0.1     0.1  90 10.0
于 2021-01-03T04:15:07.747 回答