R中的选择(使用字符)

R中的选择(使用字符),r,R,有人能告诉我这个代码有什么问题吗??? 我对这门语言还是新手我不确定你想用第一行做什么,但这正是我认为你想要的 choose = letters(1:4) f = function(x,y,choose){ if(choose == 'a'){ total = x + y } else if(choose == 'b'){ total = x - y } else if(choose == 'c'){ tot

有人能告诉我这个代码有什么问题吗???
我对这门语言还是新手

我不确定你想用第一行做什么,但这正是我认为你想要的

choose = letters(1:4)
f = function(x,y,choose){
    if(choose == 'a'){
        total = x + y
    }
    else if(choose == 'b'){
        total = x - y
    }
    else if(choose == 'c'){
        total = x * y
    }
    else{
        total = x / y
    }
    print(total)
}
f=函数(x,y,选择){
如果(选择=='a'){
总计=x+y
}
否则如果(选择=='b'){
总计=x-y
}
else if(选择=='c'){
总计=x*y
}
否则{
总计=x/y
} 
报税表(总数)
}

_值如果您试图生成字母序列,这就是您的方法

    f = function(x,y,choose){
  if(choose == 'a'){
    total = x + y
  }
  else if(choose == 'b'){
    total = x - y
  }
  else if(choose == 'c'){
    total = x * y
  }
  else{
    total = x / y
  } 
  return(total)
}

the_value<- f(5,5,"a")
print(the_value)

字母(1:4)
应该做什么?你想实现什么?你的函数对我来说很有用,但你的索引是错误的:试试
choose=letters[1:4]
letters[seq( from = 1, to = 4 )]

choose = letters[seq( from = 1, to = 4 )]
f = function(x,y,choose){
  if(choose == 'a'){
    total = x + y
  }
  else if(choose == 'b'){
    total = x - y
  }
  else if(choose == 'c'){
    total = x * y
  }
  else{
    total = x / y
  } 
  return(total)
}
print(f(5,5,choose[3]))