R语言中的ifelse条件表达语句
条件表达式:
1、
> a = 5 > ifelse(a %% 2 == 0, print("a is even"), print("a is odd")) ## 满足a %% 2 ==0, 执行第一个语句,否则执行第二个语句 [1] "a is odd" [1] "a is odd" > a = 6 > ifelse(a %% 2 == 0, print("a is even"), print("a is odd")) [1] "a is even" [1] "a is even"
2、
a = 10 ifelse(a > 0, a * 10, a * 5) ## 满足条件,则执行第一个语句, 否则执行第二个语句 a = -10 ifelse(a > 0, a * 10, a * 5)