我有一個分隔符所在的向量值,'我想找到每個文本的出現。如何解決這個問題?提前謝謝了。
val1 <- c("ab, cd, ef", "", "gh", "ab")
預期答案
ab cd ef gh
40% 20% 20% 20%
uj5u.com熱心網友回復:
在基礎 R 中:
tab <- table(trimws(unlist(strsplit(val1, ","))))
100 * tab / sum(tab)
#>
#> ab cd ef gh
#> 40 20 20 20
由reprex 包于 2022-05-10 創建(v2.0.1)
uj5u.com熱心網友回復:
library(tidyverse)
val1 %>%
str_split(pattern = ", ") %>%
unlist %>%
tibble(strings = . ) %>%
filter(strings != "") %>%
count(strings) %>%
mutate(perc = n / sum(n))
# A tibble: 4 x 3
strings n perc
<chr> <int> <dbl>
1 ab 2 0.4
2 cd 1 0.2
3 ef 1 0.2
4 gh 1 0.2
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473932.html
上一篇:如何在不輸入所有變體(在R中)的情況下搜索單詞的變體?
下一篇:拆分字串并在R中創建虛擬物件
