R: Unwanted quotation marks in tibble::lst names

本秂侑毒 提交于 2019-12-24 01:50:18

问题


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.

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.

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

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"

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

回答1:


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"

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"

or just write your own function

nlist <- function(...) {
    setNames(list(...), c(...))
}
cities <- nlist("New Haven", "Bridgeport", "Hartford") 


来源:https://stackoverflow.com/questions/52389448/r-unwanted-quotation-marks-in-tibblelst-names

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!