Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to string count unique values in data strings

Tags:

r

tidyverse

I am trying to find common words having 5 unique vowels (i.e: "aeiuo" without in a single word and without repetition)

I tried this:

library(tidyverse)
x<-c("appropriate","associate","available","colleague","experience","encourage","encouragi","associetu")
x[str_count(x,"[aeiuo]")>4]

Note that words ("encouragi" and "associetu") were used for the purpose of verifying my intended answer

the results I am generating are the following:

[3] "available"   "colleague"  
[5] "experience"  "encourage"  
[7] "encouragi"   "associetu"

While I wanted to get only: "encouragi" "associetu" which fulfill the criteria of having 5 distinct vowels (i.e: "aeiuo").

Is there any function to serve as string_count_unique?? if yes, which one? if not, what other function might you recommend me to use so that I meet the set criteria?

thank you in advance for your help!

like image 589
Birasafab Avatar asked Mar 02 '23 18:03

Birasafab


2 Answers

One option could be:

x[lengths(lapply(str_extract_all(x, "a|e|i|u|o"), unique)) == 5]

[1] "encouragi" "associetu"
like image 196
tmfmnk Avatar answered Mar 11 '23 22:03

tmfmnk


Maybe strsplit could help you

> x[sapply(strsplit(x,""),function(v) sum(unique(v)%in%c("a","e","i","o","u"))>4) ]
[1] "encouragi" "associetu"
like image 35
ThomasIsCoding Avatar answered Mar 11 '23 22:03

ThomasIsCoding