I want to create a new variable in a specific place. I can create a variable with mutate
and then reorder with select
, but I would rather do it tibble:add_column
.
This is a simple example with aperture dataset:
library(tidyverse)
iris %>% mutate(With_mutate = ifelse(Sepal.Length > 4 & Sepal.Width > 3 , TRUE, FALSE)) %>%
select(Sepal.Length:Petal.Width, With_mutate, everything()) %>%
head()
iris %>% add_column(With_add_column = "Test", .before = "Species") %>%
head()
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
I would really appreciate it if someone could tell me why my operator is ifelse
not working with add_column
.
source
share