将条件语句对象调用ifelse

时间:2015-11-27 16:06:24

标签: r if-statement

首先,我想创建包含条件语句的对象。然后,我想把这句话称为ifelse。在此先感谢您的帮助!

示例:

library(dplyr)

dd <- iris

# This code does the job but it gets messy when I have many statements
dd <- dd %>%
  mutate(Box = 
           ifelse(Sepal.Length > 5 & Species == "setosa", "Box_1", NA))


# This code doesn't work but it's what I'd aim
st <- Sepal.Length > 5 & Species == "setosa"

dd <- dd %>%
  mutate(Box = 
           ifelse(st, "Box_1", NA))

0 个答案:

没有答案