我希望在特定位置创建一个新的变量。我可以使用mutate
创建变量,然后使用select
重新排序,但我更喜欢使用tibble:add_column
的方式来完成。
这是在鸢尾花数据集上进行的一个简单示例:
library(tidyverse)
## This works fine
iris %>% mutate(With_mutate = ifelse(Sepal.Length > 4 & Sepal.Width > 3 , TRUE, FALSE)) %>%
select(Sepal.Length:Petal.Width, With_mutate, everything()) %>%
head()
## This works also
iris %>% add_column(With_add_column = "Test", .before = "Species") %>%
head()
## This doesn't work
iris %>% add_column(With_add_column = ifelse(Sepal.Length > 4 & Sepal.Width > 3 , TRUE, FALSE), .before = "Species") %>%
head()
Error in ifelse(Sepal.Length > 2 & Sepal.Width > 1, TRUE, FALSE) :
object 'Sepal.Length' not found
我希望有人能告诉我为什么我的
ifelse
语句在使用add_column
时无法正常工作。
ifelse
。 - Carpatorus