在嵌套的 tibble R 中重命名数据列时,取消引用无法在 mutate 和 map2 中找到变量
Unquoting fails to find variable in mutate and map2 when renaming column of data in nested tibble R
好的,我只是想根据 identifier/character 列重命名嵌套 tibble 中的列:
MWE:
library(magrittr)
iris %>%
tibble::as_tibble() %>%
tidyr::nest(-Species) %>%
dplyr::mutate(
Species = as.character(Species),
data = purrr::map2(data, Species,
~dplyr::rename(.x, !!.y := Sepal.Width)))
但是这个 returns 错误:
Error in quos(..., .named = TRUE) : object '.y' not found
我尝试使用 rlang
中的 ensym
以及 !!
和 :=
的各种组合,但都没有成功。
也就是说,数据列中的第一个 tibble 应将 Sepal.Width 列重命名为 setosa,第二个为 versicolor,最后一个 tibble Sepal.Widht 应重命名为 virginica。
library("tidyverse")
rename_func <- function(data, Species) {
Species <- as.character(Species)
data %>%
rename(!!Species := Sepal.Length)
}
iris2 <- as_tibble(iris) %>%
nest(-Species) %>%
group_by(Species) %>%
mutate(
data = map2(data, Species, rename_func))
iris2 %>% filter(Species == "setosa") %>% unnest() %>% head(1)
#> # A tibble: 1 x 5
#> # Groups: Species [3]
#> Species setosa Sepal.Width Petal.Length Petal.Width
#> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 setosa 5.1 3.5 1.4 0.2
iris2 %>% filter(Species == "versicolor") %>% unnest() %>% head(1)
#> # A tibble: 1 x 5
#> # Groups: Species [3]
#> Species versicolor Sepal.Width Petal.Length Petal.Width
#> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 versicolor 7 3.2 4.7 1.4
iris2 %>% filter(Species == "virginica") %>% unnest() %>% head(1)
#> # A tibble: 1 x 5
#> # Groups: Species [3]
#> Species virginica Sepal.Width Petal.Length Petal.Width
#> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 virginica 6.3 3.3 6 2.5
由 reprex package (v0.2.1)
于 2019-03-10 创建
您可以不使用公式表示法:
library(magrittr)
irisNest <- iris %>%
tibble::as_tibble() %>%
tidyr::nest(-Species) %>%
dplyr::mutate(Species = as.character(Species))
f <- function(x,y) {dplyr::rename(x, !!y := Sepal.Width)}
irisCheck <- dplyr::mutate(irisNest,
data = purrr::map2(data, Species, f))
好的,我只是想根据 identifier/character 列重命名嵌套 tibble 中的列:
MWE:
library(magrittr)
iris %>%
tibble::as_tibble() %>%
tidyr::nest(-Species) %>%
dplyr::mutate(
Species = as.character(Species),
data = purrr::map2(data, Species,
~dplyr::rename(.x, !!.y := Sepal.Width)))
但是这个 returns 错误:
Error in quos(..., .named = TRUE) : object '.y' not found
我尝试使用 rlang
中的 ensym
以及 !!
和 :=
的各种组合,但都没有成功。
也就是说,数据列中的第一个 tibble 应将 Sepal.Width 列重命名为 setosa,第二个为 versicolor,最后一个 tibble Sepal.Widht 应重命名为 virginica。
library("tidyverse")
rename_func <- function(data, Species) {
Species <- as.character(Species)
data %>%
rename(!!Species := Sepal.Length)
}
iris2 <- as_tibble(iris) %>%
nest(-Species) %>%
group_by(Species) %>%
mutate(
data = map2(data, Species, rename_func))
iris2 %>% filter(Species == "setosa") %>% unnest() %>% head(1)
#> # A tibble: 1 x 5
#> # Groups: Species [3]
#> Species setosa Sepal.Width Petal.Length Petal.Width
#> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 setosa 5.1 3.5 1.4 0.2
iris2 %>% filter(Species == "versicolor") %>% unnest() %>% head(1)
#> # A tibble: 1 x 5
#> # Groups: Species [3]
#> Species versicolor Sepal.Width Petal.Length Petal.Width
#> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 versicolor 7 3.2 4.7 1.4
iris2 %>% filter(Species == "virginica") %>% unnest() %>% head(1)
#> # A tibble: 1 x 5
#> # Groups: Species [3]
#> Species virginica Sepal.Width Petal.Length Petal.Width
#> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 virginica 6.3 3.3 6 2.5
由 reprex package (v0.2.1)
于 2019-03-10 创建您可以不使用公式表示法:
library(magrittr)
irisNest <- iris %>%
tibble::as_tibble() %>%
tidyr::nest(-Species) %>%
dplyr::mutate(Species = as.character(Species))
f <- function(x,y) {dplyr::rename(x, !!y := Sepal.Width)}
irisCheck <- dplyr::mutate(irisNest,
data = purrr::map2(data, Species, f))