简体   繁体   English

在 dplyr::mutate 中使用名称向量和用于计算的向量创建几个新变量

[英]Create several new variables using a vector of names and a vector for computation within dplyr::mutate

I'd like to create several new columns.我想创建几个新列。 They should take their names from one vector and they should be computed by taking one column in the data and dividing it by another.它们的名字应该取自一个向量,并且它们应该通过取数据中的一列并将其除以另一列来计算。

mytib <- tibble(id = 1:2, value1 = c(4,6), value2 = c(42, 5), total = c(2,2))
myvalues <- c("value1", "value2")
mynames  <- c("value1_percent", "value2_percent")

mytib %>%
     mutate({{ mynames }} := {{ myvalues }}/total)

Here, I get the error message, which makes me think that the curly-curly operator is misplaced在这里,我收到错误消息,这让我觉得 curly-curly 运算符放错了地方

Error in local_error_context(dots = dots, .index = i, mask = mask): promise already under evaluation: recursive default argument reference or earlier problems? local_error_context(dots = dots, .index = i, mask = mask) 中的错误:promise 已在评估中:递归默认参数引用或更早的问题?

I'd like to calculate the percentage columns programmatically (since I have many such columns in my data).我想以编程方式计算百分比列(因为我的数据中有很多这样的列)。

The desired output should be equivalent to this:所需的 output 应等效于此:

mytib %>%
    mutate( "value1_percent" = value1/total, "value2_percent" = value2/total)

which gives这使

# A tibble: 2 × 6
     id value1 value2 total value1_percent value2_percent
  <int>  <dbl>  <dbl> <dbl>          <dbl>          <dbl>
1     1      4     42     2              2           21  
2     2      6      5     2              3            2.5

You could use across and construct the new names in its .names argument:您可以across.names参数中使用并构造新名称:

library(dplyr)

mytib %>%
    mutate(across(starts_with('value'),
                  ~ .x / total,
                  .names = "{.col}_percent"
                  ))

I prefer mutate(across(...)) in this case.在这种情况下,我更喜欢mutate(across(...)) To make your idea work, try reduce2() from purrr .为了使您的想法可行,请尝试使用 purrr 中的purrr reduce2()

library(dplyr)
library(purrr)

reduce2(mynames, myvalues,
        ~ mutate(..1, !!..2 := !!sym(..3)/total), .init = mytib)

# # A tibble: 2 x 6
#      id value1 value2 total value1_percent value2_percent
#   <int>  <dbl>  <dbl> <dbl>          <dbl>          <dbl>
# 1     1      4     42     2              2           21  
# 2     2      6      5     2              3            2.5

The above code is actually a shortcut of:上面的代码实际上是一个快捷方式:

mytib %>%
  mutate(!!mynames[1] := !!sym(myvalues[1])/total,
         !!mynames[2] := !!sym(myvalues[2])/total)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 `dplyr::mutate()` 从向量中指定的名称创建几个新变量 - Using `dplyr::mutate()` to create several new variables from names specified in a vector 使用dplyr mutate根据列名向量创建新列 - use dplyr mutate to create new columns based on a vector of column names dplyr使用列名的字符向量进行变异 - dplyr mutate using character vector of column names 将列名向量传递给 mutate (dplyr) 中的 paste() - Pass vector of column names to paste() within mutate (dplyr) 使用dplyr用新名称替换名称向量 - Using dplyr to replace a vector of names with new names 在dplyr中使用mutate()时,如何在不键入所有名称的情况下在行中创建新列作为总和或均值或几个单元格? - When using mutate() in dplyr, how to create a new column to be the sum or mean or several cells in the row without typing all the names? 使用 dplyr::mutate 根据字符串向量(或 tidyselect)传递的多个条件和相应的变量名称创建新变量 - Creating new variable with dplyr::mutate based on multiple conditions and corresponding variable names passed by string vector (or tidyselect) R dplyr :: mutif使用ifelse和%in%无法创建因子(在mutate_impl中:绑定字符和因子向量,强制转换为字符向量) - R dplyr::mutate using ifelse and %in% cannot create a factor (In mutate_impl: binding character and factor vector, coercing into character vector) 当列的值是向量的名称时使用 dplyr mutate - Use dplyr mutate when the values of a column are the names of a vector dplyr mutate_at:使用重新编码创建新变量 - dplyr mutate_at: create new variables with recode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM