pb with str_count – Posit Forum (formerly RStudio Community)

Here’s what I get on my install, a System76 version of Ubuntu 22.04 with version details as shown, using your example and the stringr help(str_count) examples. All appears as it should. What is very odd is that one or both of the two arguments, both shown as strings, which are vectors, are being parsed as environments. In a clean R session there should only be the .Global environment and, within the str_count() function a local environment. I tried giving an environment object as an argument, though, and got a different message.

e1 <- new.env(parent = baseenv()) 
assign("a", 3, envir = e1)
ls(e1)
#> [1] "a"
class(e1)
#> [1] "environment"
stringr::str_count(e1,"a")
#> Error in stri_count_regex(string, pattern, opts_regex = opts(pattern)): argument `str` should be a character vector (or an object coercible to)

library(stringr)
str_count("ABCD","A")
#> [1] 1

fruit <- c("apple", "banana", "pear", "pineapple")
str_count(fruit, "a")
#> [1] 1 3 1 1
str_count(fruit, "p")
#> [1] 2 0 1 3
str_count(fruit, "e")
#> [1] 1 0 1 2
str_count(fruit, c("a", "b", "p", "p"))
#> [1] 1 1 1 3

str_count(c("a.", "...", ".a.a"), ".")
#> [1] 2 3 4
str_count(c("a.", "...", ".a.a"), fixed("."))
#> [1] 1 3 2

sessionInfo()
#> R version 4.2.0 (2022-04-22)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Pop!_OS 22.04 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/local/lib/R/lib/libRblas.so
#> LAPACK: /usr/local/lib/R/lib/libRlapack.so
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] stringr_1.4.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] rstudioapi_0.13   knitr_1.39        magrittr_2.0.3    R.cache_0.15.0   
#>  [5] rlang_1.0.4       fastmap_1.1.0     fansi_1.0.3       styler_1.7.0     
#>  [9] highr_0.9         tools_4.2.0       xfun_0.32         R.oo_1.24.0      
#> [13] utf8_1.2.2        cli_3.3.0         withr_2.5.0       htmltools_0.5.3  
#> [17] yaml_2.3.5        digest_0.6.29     tibble_3.1.8      lifecycle_1.0.1  
#> [21] purrr_0.3.4       R.utils_2.10.1    vctrs_0.4.1       fs_1.5.2         
#> [25] glue_1.6.2        evaluate_0.16     rmarkdown_2.15    reprex_2.0.1     
#> [29] stringi_1.7.8     compiler_4.2.0    pillar_1.8.0      R.methodsS3_1.8.1
#> [33] pkgconfig_2.0.3

Created on 2023-01-31 by the reprex package (v2.0.1)

Read more here: Source link