问题
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.
回答1:
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'
.
来源:https://stackoverflow.com/questions/37891767/r-a-function-to-tell-whether-a-single-char-is-vowel-or-not