我正在尝试使用case_when,如下所示:
x <- 22
y <- runif(1)
z <- case_when (
x <= 22 ~ "FT",
x > 22 & x <= 25 & y < 0.5 ~ "FT",
x > 22 & x <= 25 & y >= 0.5 ~ "PT",
x > 25 & y > 0.9 ~ "PT",
X > 25 & y <= 0.9 ~ "FT",
TRUE ~ "PT")
运行此命令我会得到
eval_bare(f [[2]],env)中的错误:找不到对象'X'
还存在x
x
#[1] 22
答案 0 :(得分:1)
正如莫里斯(Maurits)所说,您的case_when
陈述中有错字。将大写的最后一个“ X”更改为“ x”
z <- case_when (
x <= 22 ~ "FT",
x > 22 & x <= 25 & y < 0.5 ~ "FT",
x > 22 & x <= 25 & y >= 0.5 ~ "PT",
x > 25 & y > 0.9 ~ "PT",
x > 25 & y <= 0.9 ~ "FT",
TRUE ~ "PT")
z
[1] "FT"