Jess1
Jess1

Reputation: 31

R: A function to tell whether a single char is vowel or not

I'm dealing with if in R and I'm struggling with one of the typical examples: checking if it is a vowel= TRUE and FALSE otherwise.

if.function <- function(char){
  if (char=('a') or ('e') or ('i') or ('o') or ('u') ) 
    {
    return(TRUE)
  } else if (char == 0){
    return(FALSE)
  }

Could someone give me a hand?

I saw other examples with Python and Java, but I barely know how to use just R.

Upvotes: 2

Views: 2226

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73385

char=('a') or ('e') or ('i') or ('o') or ('u') is illegal. Try

isVowel <- function(char) char %in% c('a', 'e', 'i', 'o', 'u')

Let's try it:

isVowel('a')
# [1] TRUE
isVowel('b')
# [1] FALSE

Note that I did not use the or operator '||':

char == 'a' || char == 'e' || char == 'i' || char == 'o' || char == 'u'

as this is too long. I have used

char %in% c('a', 'e', 'i', 'o', 'u')

This will give TRUE if char is any of 'a', 'e', 'i', 'o', 'u'.

Upvotes: 7

Related Questions