ifelse()函数中的多个动作

时间:2017-07-11 12:20:25

标签: r if-statement vector

我正在寻找答案,但我找不到任何答案。可以在ifelse()函数中放置多个动作吗?或者我必须找到其他解决方案?

示例:

vec <- c(1,3,4,6,8,4,2,4,6,8)
ifelse(vec %% 2 == 0, "even", "odd")
[1] "odd"  "odd"  "even" "even" "even" "even" "even" "even" "even" "even"

现在我想打印“奇数”或“偶数”,当它甚至我想要打印偶数元素时,当它奇怪时我想打印增加的矢量元素1:

ifelse(vec %% 2 == 0, "even"; vec, "odd"; vec+1)

期望的结果:

[1] "odd"  "odd"  "even" "even" "even" "even" "even" "even" "even" "even"
> vec
 [1] 2 4 4 6 8 4 2 4 6 8

0 个答案:

没有答案