[英]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.