R:查看两个简单字符串的唯一元素时出现奇怪的结果
R: strange results when looking at the unique elements of two simple strings
我对我所看到的感到非常困惑。
我读了一个 excel 文件,当我查看一列字符串中的唯一值时,我不明白结果。
我可以用最小的 reprex 重现它(见下文):为什么 dd 有两个独特的元素,而 dd2 只有一个?
如有任何建议,我们将不胜感激。
dd <- c("Grant", "Grant")
dd2 <- c("Grant", "Grant")
unique(dd)
#> [1] "Grant" "Grant"
length(unique(dd))
#> [1] 2
unique(dd2)
#> [1] "Grant"
length(unique(dd2))
#> [1] 1
sessionInfo()
#> R version 4.1.1 (2021-08-10)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux 11 (bullseye)
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
#>
#> locale:
#> [1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
#> [5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
#> [7] LC_PAPER=en_GB.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> loaded via a namespace (and not attached):
#> [1] knitr_1.33 magrittr_2.0.1 rlang_0.4.11 fansi_0.5.0
#> [5] stringr_1.4.0 styler_1.5.1 highr_0.9 tools_4.1.1
#> [9] xfun_0.25 utf8_1.2.2 withr_2.4.2 htmltools_0.5.1.1
#> [13] ellipsis_0.3.2 yaml_2.2.1 digest_0.6.27 tibble_3.1.3
#> [17] lifecycle_1.0.0 crayon_1.4.1 purrr_0.3.4 vctrs_0.3.8
#> [21] fs_1.5.0 glue_1.4.2 evaluate_0.14 rmarkdown_2.10
#> [25] reprex_2.0.1 stringi_1.7.3 compiler_4.1.1 pillar_1.6.2
#> [29] backports_1.2.1 pkgconfig_2.0.3
由 reprex package (v2.0.1)
于 2021-09-13 创建
原始值似乎不同,可能是复制而来[=16=]
sapply(dd, charToRaw)
$`Grant`
[1] ef bb bf 47 72 61 6e 74
$Grant
[1] 47 72 61 6e 74
而 dd2
是一样的
sapply(dd2, charToRaw)
Grant Grant
[1,] 47 47
[2,] 72 72
[3,] 61 61
[4,] 6e 6e
[5,] 74 74
第一种情况好像多了一个字符
nchar(dd)
[1] 6 5
如果我们删除第一个字符,unique
将是 1
unique(c(substring(dd[1],2), dd[2]))
[1] "Grant"
我对我所看到的感到非常困惑。 我读了一个 excel 文件,当我查看一列字符串中的唯一值时,我不明白结果。
我可以用最小的 reprex 重现它(见下文):为什么 dd 有两个独特的元素,而 dd2 只有一个?
如有任何建议,我们将不胜感激。
dd <- c("Grant", "Grant")
dd2 <- c("Grant", "Grant")
unique(dd)
#> [1] "Grant" "Grant"
length(unique(dd))
#> [1] 2
unique(dd2)
#> [1] "Grant"
length(unique(dd2))
#> [1] 1
sessionInfo()
#> R version 4.1.1 (2021-08-10)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux 11 (bullseye)
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
#>
#> locale:
#> [1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
#> [5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
#> [7] LC_PAPER=en_GB.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> loaded via a namespace (and not attached):
#> [1] knitr_1.33 magrittr_2.0.1 rlang_0.4.11 fansi_0.5.0
#> [5] stringr_1.4.0 styler_1.5.1 highr_0.9 tools_4.1.1
#> [9] xfun_0.25 utf8_1.2.2 withr_2.4.2 htmltools_0.5.1.1
#> [13] ellipsis_0.3.2 yaml_2.2.1 digest_0.6.27 tibble_3.1.3
#> [17] lifecycle_1.0.0 crayon_1.4.1 purrr_0.3.4 vctrs_0.3.8
#> [21] fs_1.5.0 glue_1.4.2 evaluate_0.14 rmarkdown_2.10
#> [25] reprex_2.0.1 stringi_1.7.3 compiler_4.1.1 pillar_1.6.2
#> [29] backports_1.2.1 pkgconfig_2.0.3
由 reprex package (v2.0.1)
于 2021-09-13 创建原始值似乎不同,可能是复制而来[=16=]
sapply(dd, charToRaw)
$`Grant`
[1] ef bb bf 47 72 61 6e 74
$Grant
[1] 47 72 61 6e 74
而 dd2
是一样的
sapply(dd2, charToRaw)
Grant Grant
[1,] 47 47
[2,] 72 72
[3,] 61 61
[4,] 6e 6e
[5,] 74 74
第一种情况好像多了一个字符
nchar(dd)
[1] 6 5
如果我们删除第一个字符,unique
将是 1
unique(c(substring(dd[1],2), dd[2]))
[1] "Grant"