Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make every other vowel uppercase in R

Given a lower case string. Ex:

s <- 'abcdefghijklmnopqrstuvwxyz'

The goal is to make every other vowel uppercase.

Desired output here:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

abcdEfghijklmnOpqrstuvwxyz

As you can see, from aeiou, e and o get uppercased.

There are only lowercase characters in the strings in all cases.

For aeiou, the desired output is:

aEiOu

How could I do this?

I tried:

s[unlist(strsplit(s, '')) %in% c('a', 'e', 'i', 'o', 'u')] <- toupper(s[unlist(strsplit(s, '')) %in% c('a', 'e', 'i', 'o', 'u')])

But no avail.

>Solution :

It’s not a one-liner, but:

s <- 'abcdefghijklmnopqrstuvwxyz'

as_list <- unlist(strsplit(s, ''))
vowels <- as_list %in% c('a', 'e', 'i', 'o', 'u')
every_other_index <- which(vowels)[c(FALSE, TRUE)]

as_list[every_other_index] <- toupper(as_list[every_other_index])

print(paste(as_list, collapse=''))

gives:

[1] "abcdEfghijklmnOpqrstuvwxyz"

(Use of which taken from this question; use of c(FALSE, TRUE)] from here.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading