与 across 和 .names 发生变异:"glue cannot interpolate functions into strings" 错误
mutate with across and .names: "glue cannot interpolate functions into strings" error
我有不同选区候选人的选举结果。来源有每个候选人的票数和每个选区的总票数。我想为每个候选人在每个地区获得的选票百分比添加变量。
我已经成功地使用 mutate
和 across
将投票计数替换为百分比,但是在尝试使用 .names
参数创建新变量时出现错误(即我希望获得新变量,cand1_pct
、cand2_pct
、...)。
library(tidyverse)
df <- data.frame(district = 1:3,
cand1 = c(12, 2, 14),
cand2 = c(2, 6, 23),
cand3 = c(3, 16, 2),
total = c(17, 24, 39))
df %>%
mutate(across(2:4, ~ .x/total*100))
#> district cand1 cand2 cand3 total
#> 1 1 70.588235 11.76471 17.647059 17
#> 2 2 8.333333 25.00000 66.666667 24
#> 3 3 35.897436 58.97436 5.128205 39
df %>%
mutate(across(2:4, ~ .x/total*100, .names = "{.col}_pct"))
#> Error: Problem with `mutate()` input `..1`.
#> x glue cannot interpolate functions into strings.
#> * object '.col' is a function.
#> i Input `..1` is `across(2:4, ~.x/total * 100, .names = "{.col}_pct")`.
由 reprex package (v0.3.0)
于 2020-08-12 创建
我首先认为这是我对 across
和 .names
应该如何工作的误解,但是当我使用 across
vignette 我犯了同样的错误。我在我的本地机器和 RStudio 云上都试过了。 dplyr
版本 1.0.1.
library(dplyr)
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), mean, .names = "mean_{.col}"))
#> Error: Problem with `summarise()` input `..1`.
#> x glue cannot interpolate functions into strings.
#> * object '.col' is a function.
#> i Input `..1` is `across(starts_with("Sepal"), mean, .names = "mean_{.col}")`.
#> i The error occurred in group 1: Species = "setosa".
由 reprex package (v0.3.0)
于 2020-08-12 创建
根据?across
,不是.col
,只是col
.names - The default (NULL) is equivalent to "{col}" for the single function case and "{col}_{fn}" for the case where a list is used for .fns.
library(dplyr)
df %>%
mutate(across(2:4, ~ .x/total*100, .names = "{col}_pct"))
# district cand1 cand2 cand3 total cand1_pct cand2_pct cand3_pct
#1 1 12 2 3 17 70.588235 11.76471 17.647059
#2 2 2 6 16 24 8.333333 25.00000 66.666667
#3 3 14 23 2 39 35.897436 58.97436 5.128205
...情况发生了变化。
根据最新dplyr 1.0.2
中的?across
,现在建议使用.col
,而不是col
。
.names - A glue specification that describes how to name the output columns. This can use {.col} to stand for the selected column name, and {.fn} to stand for the name of the function being applied. The default (NULL) is equivalent to "{.col}" for the single function case and "{.col}_{.fn}" for the case where a list is used for .fns.
可以在此处找到更多信息和用例:
我有不同选区候选人的选举结果。来源有每个候选人的票数和每个选区的总票数。我想为每个候选人在每个地区获得的选票百分比添加变量。
我已经成功地使用 mutate
和 across
将投票计数替换为百分比,但是在尝试使用 .names
参数创建新变量时出现错误(即我希望获得新变量,cand1_pct
、cand2_pct
、...)。
library(tidyverse)
df <- data.frame(district = 1:3,
cand1 = c(12, 2, 14),
cand2 = c(2, 6, 23),
cand3 = c(3, 16, 2),
total = c(17, 24, 39))
df %>%
mutate(across(2:4, ~ .x/total*100))
#> district cand1 cand2 cand3 total
#> 1 1 70.588235 11.76471 17.647059 17
#> 2 2 8.333333 25.00000 66.666667 24
#> 3 3 35.897436 58.97436 5.128205 39
df %>%
mutate(across(2:4, ~ .x/total*100, .names = "{.col}_pct"))
#> Error: Problem with `mutate()` input `..1`.
#> x glue cannot interpolate functions into strings.
#> * object '.col' is a function.
#> i Input `..1` is `across(2:4, ~.x/total * 100, .names = "{.col}_pct")`.
由 reprex package (v0.3.0)
于 2020-08-12 创建我首先认为这是我对 across
和 .names
应该如何工作的误解,但是当我使用 across
vignette 我犯了同样的错误。我在我的本地机器和 RStudio 云上都试过了。 dplyr
版本 1.0.1.
library(dplyr)
iris %>%
group_by(Species) %>%
summarise(across(starts_with("Sepal"), mean, .names = "mean_{.col}"))
#> Error: Problem with `summarise()` input `..1`.
#> x glue cannot interpolate functions into strings.
#> * object '.col' is a function.
#> i Input `..1` is `across(starts_with("Sepal"), mean, .names = "mean_{.col}")`.
#> i The error occurred in group 1: Species = "setosa".
由 reprex package (v0.3.0)
于 2020-08-12 创建根据?across
,不是.col
,只是col
.names - The default (NULL) is equivalent to "{col}" for the single function case and "{col}_{fn}" for the case where a list is used for .fns.
library(dplyr)
df %>%
mutate(across(2:4, ~ .x/total*100, .names = "{col}_pct"))
# district cand1 cand2 cand3 total cand1_pct cand2_pct cand3_pct
#1 1 12 2 3 17 70.588235 11.76471 17.647059
#2 2 2 6 16 24 8.333333 25.00000 66.666667
#3 3 14 23 2 39 35.897436 58.97436 5.128205
...情况发生了变化。
根据最新dplyr 1.0.2
中的?across
,现在建议使用.col
,而不是col
。
.names - A glue specification that describes how to name the output columns. This can use {.col} to stand for the selected column name, and {.fn} to stand for the name of the function being applied. The default (NULL) is equivalent to "{.col}" for the single function case and "{.col}_{.fn}" for the case where a list is used for .fns.
可以在此处找到更多信息和用例: