I have a word list as in wordlist below:
wordlist <- data.frame(words = c("anywhere", "youll", "feel", "comfortable", "please", "dont"))
I have another dataframe with a list of consonants:
consonants <- data.frame(consonants = c("b", "c", "d", "f", "g", "h"))
I want to create a new variable in wordlist called word_structure, where all consonants are replaced with "C", and all vowels with "V":
wordlist$word_structure <- c("VCCCCVCV", "CVVCC", "CVVC", "CVCCVCCVCCV", "CCVVCV", "CVCC")
I can't work out how to combine conditional formatting with gsub to get what I need.
This seems a better fit for chartr() than gsub():
vowels <- c("a", "e", "i", "o", "u")
consonants <- letters[!letters %in% vowels]
wordlist$word_structure <- chartr(
old = paste(c(vowels, consonants), collapse = ""),
new = paste(c(rep("V", 5), rep("C", 21)), collapse = ""),
wordlist$words)
wordlist
words word_structure
1 anywhere VCCCCVCV
2 youll CVVCC
3 feel CVVC
4 comfortable CVCCVCCVCCV
5 please CCVVCV
6 dont CVCC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With