I have a vector value where the separator is '
and I would like to find the occurrence of each text. How to get around with this? many thanks in advance.
val1 <- c("ab, cd, ef", "", "gh", "ab")
Expected Answer
ab cd ef gh
40% 20% 20% 20%
CodePudding user response:
In base R:
tab <- table(trimws(unlist(strsplit(val1, ","))))
100 * tab / sum(tab)
#>
#> ab cd ef gh
#> 40 20 20 20
Created on 2022-05-10 by the reprex package (v2.0.1)
CodePudding user response:
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