小编Hos*_*her的帖子

R 中数组的 if 语句

以简单的方式陈述我的问题,考虑我有以下功能:

> ff<-function(a){ if (a>0){ return ("positive") } else{ return("negative") } }
Run Code Online (Sandbox Code Playgroud)

现在:

> ff(-1)
[1] "negative"
> ff(1)
[1] "positive"
Run Code Online (Sandbox Code Playgroud)

而当使用数组时:

> print(ff(c(-1,1)))
[1] "negative" "negative"
Warning message:
In if (a > 0) { :
  the condition has length > 1 and only the first element will be used
Run Code Online (Sandbox Code Playgroud)

我正期待着

print(ff(c(-1,1)))=("negative" "positive")
Run Code Online (Sandbox Code Playgroud)

我应该如何解决这个问题?

arrays if-statement r

0
推荐指数
1
解决办法
1667
查看次数

标签 统计

arrays ×1

if-statement ×1

r ×1