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

ぐ巨炮叔叔 提交于 2020-01-05 04:05:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!