R:tibble :: lst名称中不需要的引号 [英] R: Unwanted quotation marks in tibble::lst names
问题描述
我最近才了解到 tibble :: lst
,它创建一个列表对象,但会自动命名列表项。我将其用作%>%
工作流程中的快捷方式,该工作流程将名称用作 .id
map_dfr
中的参数,因此自动命名确实很有用。
I only recently learned of tibble::lst
, which creates a list object but automatically names list items. I'm using this as a shortcut within a %>%
workflow that makes use of the names as the .id
argument in map_dfr
, so the automatic naming is really helpful.
但是,名称中带有引号。我注意到这一点是因为它们笨拙地在 ggplot
的轴刻度标签中打印,即我有一个标签,上面写着 Hartford
而不是 Hartford
。
However, the names are coming in with quotation marks around them. I noticed this because they awkwardly printed in axis tick labels in a ggplot
, i.e. I had a label saying "Hartford"
instead of Hartford
.
我浏览了 tidyverse / tibble
github上的问题,但没有找到任何东西。这是一个错误,还是我做错了什么?
I looked through issues on the tidyverse/tibble
github but didn't find anything. Is this a bug, or am I doing something wrong?
library(dplyr)
library(purrr)
cities <- lst("New Haven", "Bridgeport", "Hartford")
cities
#> $`"New Haven"`
#> [1] "New Haven"
#>
#> $`"Bridgeport"`
#> [1] "Bridgeport"
#>
#> $`"Hartford"`
#> [1] "Hartford"
cities %>%
map_dfr(~tibble(dummy = rnorm(1)), .id = "city")
#> # A tibble: 3 x 2
#> city dummy
#> <chr> <dbl>
#> 1 "\"New Haven\"" -0.956
#> 2 "\"Bridgeport\"" 0.533
#> 3 "\"Hartford\"" -0.0553
起初我以为是逃避 New Haven中的空格,但也会出现单个字符:
At first I thought it might be to escape the space in "New Haven", but it happens with single characters as well:
lst("a", "b", "c")
#> $`"a"`
#> [1] "a"
#>
#> $`"b"`
#> [1] "b"
#>
#> $`"c"`
#> [1] "c"
当我提供姓名时,它的工作原理与我预期的一样,但是却挫败了这一优势第一
在基本列表
上。
It works as I expect when I provide names, but that defeats this advantage that lst
has over the base list
.
lst(a = "a", b = "b", c = "c")
#> $a
#> [1] "a"
#>
#> $b
#> [1] "b"
#>
#> $c
#> [1] "c"
我很确定我是最新的 tidyverse
相关的软件包,但这是我的会话信息,以防万一:
Pretty sure I'm up to date on tidyverse
-related packages, but here's my session info just in case:
sessionInfo()
#> R version 3.5.1 (2018-07-02)
#> Platform: x86_64-apple-darwin15.6.0 (64-bit)
#> Running under: macOS High Sierra 10.13.6
#>
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/3.5/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] purrr_0.2.5 dplyr_0.7.6
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_0.12.18 knitr_1.20 bindr_0.1.1 magrittr_1.5
#> [5] tidyselect_0.2.4 R6_2.2.2 rlang_0.2.2 fansi_0.3.0
#> [9] stringr_1.3.1 tools_3.5.1 utf8_1.1.4 cli_1.0.0
#> [13] htmltools_0.3.6 yaml_2.2.0 assertthat_0.2.0 rprojroot_1.3-2
#> [17] digest_0.6.16 tibble_1.4.2 crayon_1.3.4 bindrcpp_0.2.2
#> [21] glue_1.3.0 evaluate_0.11 rmarkdown_1.10 stringi_1.2.4
#> [25] compiler_3.5.1 pillar_1.3.0 backports_1.1.2 pkgconfig_2.0.2
推荐答案
lst()
实际上是与变量一起使用的。例如
lst()
is really meant to be used with variables. Such as
xa<-"a"
xb<-"b"
xc<-"c"
lst(xa,xb,xc)
# $`xa`
# [1] "a"
# $xb
# [1] "b"
# $xc
# [1] "c"
它不能与文字,未命名的值配合使用。它从您传入的未求值表达式中获取元素的名称。因此,如果您传递一个字符值,则该求值表达式仍带有引号。我想您只想在这里 list()
。可能的名称:
It doesn't play well with literal, unnamed values. It takes the name of the element from the unevaluated expression you pass in. So if you pass in a character value, that evaluated expression still has the quotes. I think you just want list()
here. Possibly with names:
cities <- list("New Haven", "Bridgeport", "Hartford")
names(cities)<-unname(cities)
cities
# $`New Haven`
# [1] "New Haven"
# $Bridgeport
# [1] "Bridgeport"
# $Hartford
# [1] "Hartford"
或只编写自己的函数
nlist <- function(...) {
setNames(list(...), c(...))
}
cities <- nlist("New Haven", "Bridgeport", "Hartford")
这篇关于R:tibble :: lst名称中不需要的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!