R管道不适用于stringR的str_extract_all()

问题描述 投票:0回答:1

我遇到了一个奇怪而烦人的事情:使用stringR的管道无法正常工作:

# lets get the 5th number-block out of a string

# with piping -> error
"Test region     test    1235   45   245   2345   1432   1432" %>% 
  str_extract_all('[:digit:]+', 
                  simplify = TRUE)[,5]

类型(模式)错误:缺少参数“模式”,没有默认值

# do the same without piping -> works
  str_extract_all("Test region     test    1235   45   245   2345   1432   1432", 
                  '[:digit:]+', 
                  simplify = TRUE)[,5]

[1]“ 1432”

任何想法可能是什么问题?我花了很长时间才发现这是问题所在-.-

> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] pdftools_2.3.1  XML_3.99-0.3    forcats_0.5.0   stringr_1.4.0   dplyr_0.8.5     purrr_0.3.4     readr_1.3.1     tidyr_1.0.2     tibble_3.0.1    ggplot2_3.3.0  
[11] tidyverse_1.3.0 readxl_1.3.1   

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6     cellranger_1.1.0 pillar_1.4.3     compiler_4.0.0   dbplyr_1.4.3     tools_4.0.0      digest_0.6.25    lubridate_1.7.8  jsonlite_1.6.1  
[10] lifecycle_0.2.0  nlme_3.1-147     gtable_0.3.0     lattice_0.20-41  pkgconfig_2.0.3  rlang_0.4.5      reprex_0.3.0     cli_2.0.2        DBI_1.1.0       
[19] rstudioapi_0.11  haven_2.2.0      withr_2.2.0      xml2_1.3.2       httr_1.4.1       askpass_1.1      fs_1.4.1         generics_0.0.2   vctrs_0.2.4     
[28] hms_0.5.3        grid_4.0.0       tidyselect_1.0.0 glue_1.4.0       qpdf_1.1         R6_2.4.1         fansi_0.4.1      farver_2.0.3     modelr_0.1.6    
[37] magrittr_1.5     backports_1.1.6  scales_1.1.0     ellipsis_0.3.0   rvest_0.3.5      assertthat_0.2.1 colorspace_1.4-1 labeling_0.3     utf8_1.1.4      
[46] stringi_1.4.6    munsell_0.5.0    broom_0.5.6      crayon_1.3.4  
r tidyverse stringr magrittr
1个回答
0
投票
我们可以使用

"Test region test 1235 45 245 2345 1432 1432" %>% str_extract_all('[:digit:]+', simplify = TRUE) %>% .[,5]

或在{}中将其屏蔽
© www.soinside.com 2019 - 2024. All rights reserved.