我正在尝试使用dplyr::mutate()
和dplyr::case_when()
在数据框中创建新的数据列,该数据列使用存储在另一个对象(“查找列表”)中的数据填充,并且基于数据框中列中的信息。
我知道答案可能与正确使用准引号和NSE有关,但是我无法将Programming with dplyr
vignette中的信息推断出我的情况。
我希望在此处发布此reprex可以引导我找到正确的答案,并且我认为解决此问题将大大帮助我了解NSE。
key_list <- list(
"a" = list(
foo = 1,
bar = 2),
"b" = list(
foo = 3,
bar = 4),
"c" = list(
foo = 5,
bar = 6)
)
x <- tibble(fruit = c("apple", "orange", "grape", "apple", "apple", "orange"),
`Old Letter` = c("a", "a", "b", "c", "c", "c"),
`Old Number` = c(9, 8, 7, 6, 5, 4)
)
x
# # A tibble: 6 x 3
# fruit `Old Letter` `Old Number`
# <chr> <chr> <dbl>
# 1 apple a 9
# 2 orange a 8
# 3 grape b 7
# 4 apple c 6
# 5 apple c 5
# 6 orange c 4
具体地说,我想在x
中创建一个新列(我将称为`New Number`
),该列基于x$fruit
和x$`Old Letter`
中的值进行填充。 / p>
以下是在我的实际用例中挂断电话的代码:
x %>% mutate(`New Number` = case_when(
fruit == "apple" ~ pluck(key_list, `Old Letter`, "foo") * 10,
fruit == "orange" ~ pluck(key_list, `Old Letter`, "foo") * 100,
fruit == "grape" ~ pluck(key_list, `Old Letter`, "foo") * 1000
))
# Error: Index 1 must have length 1, not 6
在我的脑海中,我看到这样的(所需)操作顺序,例如x
的第一行:
fruit == "apple"
为TRUE,因此请评估以下表达式:pluck(key_list, `Old Letter`, "foo") * 10
`Old Letter`
列中的值为"a"
,因此表达式变为pluck(key_list, "a", "foo") * 10
(在全局环境中,该表达式应在key_list
对象上运行)< / li>
2 * 10
,等于20
`New Number`
列中。将其推算到整个命令中,我希望将其作为输出:
# # A tibble: 6 x 4
# fruit `Old Letter` `Old Number` `New Number`
# <chr> <chr> <dbl> <dbl>
# 1 apple a 9 20
# 2 orange a 8 200
# 3 grape b 7 4000
# 4 apple c 6 60
# 5 apple c 5 60
# 6 orange c 4 600
从我收到的错误消息来看,似乎不是`Old Letter`
列中的单个值被用作pluck()
的索引,而是整个`Old Letter`
列将作为向量传递。我猜这是因为根据documentation for case_when()
:
case_when()
不是一个简单的评估函数。
我试图跟踪这种情况的发生,但是跟踪堆栈似乎没有指向我任何有用的地方,并且没有将整个命令包装在rlang::qq_show()
或quo()
中向我展示R是如何根据NSE解释该命令的,因为它们也都抛出了相同的错误。
我已经尝试过以下组合:
quo()
,enquo()
,!!
,!!enquo()
(简称为{{}}
)和sym()
在上面的Reprex
代码中,以及将其包装到一个函数中,但是会引发相同的错误:
get_num <- function(x, y) purrr::pluck(key_list, x, y)
x %>% mutate(`New Number` = case_when(
fruit == "apple" ~ get_num(`Old Letter`, "foo") * 10,
fruit == "orange" ~ get_num(`Old Letter`, "foo") * 100,
fruit == "grape" ~ get_num(`Old Letter`, "foo") * 1000
))
# Error: Index 1 must have length 1, not 6
This answer关于SO的另一个问题告诉我
我猜想您对
case_when()
缺少的是,对参数进行一次评估,而不是对每一行进行评估。
但是我不确定这是否/如何适用于我的情况,所以我很茫然。
无论如何,感谢大家提供的帮助!
sessionInfo()
:R version 3.6.0 (2019-04-26)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS 10.15
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rlang_0.4.1 readxl_1.3.1 forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3 purrr_0.3.3 readr_1.3.1 tidyr_1.0.0 tibble_2.1.3
[10] ggplot2_3.2.1 tidyverse_1.2.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.2 cellranger_1.1.0 pillar_1.4.2 compiler_3.6.0 base64enc_0.1-3 tools_3.6.0 digest_0.6.22 zeallot_0.1.0 evaluate_0.14
[10] lubridate_1.7.4 jsonlite_1.6 lifecycle_0.1.0 nlme_3.1-141 gtable_0.3.0 lattice_0.20-38 pkgconfig_2.0.3 cli_1.1.0 rstudioapi_0.10
[19] yaml_2.2.0 haven_2.1.1 xfun_0.10 withr_2.1.2 xml2_1.2.2 httr_1.4.1 knitr_1.25 generics_0.0.2 vctrs_0.2.0
[28] hms_0.5.1 grid_3.6.0 tidyselect_0.2.5 glue_1.3.1 R6_2.4.0 fansi_0.4.0 rmarkdown_1.16 modelr_0.1.5 magrittr_1.5
[37] htmltools_0.4.0 backports_1.1.5 scales_1.0.0 rvest_0.3.4 assertthat_0.2.1 colorspace_1.4-1 utf8_1.1.4 stringi_1.4.3 lazyeval_0.2.2
[46] munsell_0.5.0 broom_0.5.2 crayon_1.3.4
答案 0 :(得分:3)
我认为,该问题与NSE的关系可能少于pluck
未被向量化的事实-因为当前编写的pluck
并非每行进行一次评估,而是尝试将所有行一次运行pluck
。但是,正如您所了解的,pluck
需要输入一个数字,而不是一个向量。
解决此问题的一种方法是在代码中跨行map
,将您的代码用作lambda样式的小函数。请注意,您需要使用map_dbl
来强制转换数字值,否则map
将返回一个列表,并且所有内容都会爆炸:-)
x %>%
mutate(`New Number` = case_when(
fruit == "apple" ~ map_dbl(`Old Letter`, ~ pluck(key_list, ., "foo")) * 10,
fruit == "orange" ~ map_dbl(`Old Letter`, ~ pluck(key_list, ., "foo")) * 100,
fruit == "grape" ~ map_dbl(`Old Letter`, ~ pluck(key_list, ., "foo")) * 1000
))
# # A tibble: 6 x 4
# fruit `Old Letter` `Old Number` `New Number`
# <chr> <chr> <dbl> <dbl>
# 1 apple a 9 10
# 2 orange a 8 100
# 3 grape b 7 3000
# 4 apple c 6 50
# 5 apple c 5 50
# 6 orange c 4 500