Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将条件语句对象调用到ifelse中_R_If Statement - Fatal编程技术网

R 将条件语句对象调用到ifelse中

R 将条件语句对象调用到ifelse中,r,if-statement,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

首先,我想创建一个包含条件语句的对象。然后,我想把这个语句称为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))
库(dplyr)
dd 5和物种==“刚毛”,“盒子1”,NA)
#这个代码不起作用,但这是我的目标
st 5和物种==“刚毛”
dd%
变异(框=
ifelse(st,“盒子1”,NA))

您可以执行类似于
st
Sepal的操作。Length
不是现有对象<代码>dd$萼片。长度为。您可以在每一列前面加上
dd$
,也可以像@Heroka所说的那样在
前面加上
。谢谢您的评论。Heroka,“with”工作得很好,谢谢!