Here is an example of how you will retain it. You can call mutate which will then change or create a new column in your data frame. Here you see we create z based on column_a.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df <- data.frame(
"column_a" = 1:10,
"column_b" = 2:11
)
df <- df %>%
mutate(z =
dplyr::case_when(
column_a <= 5 ~ 1,
column_a >= 6 ~ 2
)
)
df
#> column_a column_b z
#> 1 1 2 1
#> 2 2 3 1
#> 3 3 4 1
#> 4 4 5 1
#> 5 5 6 1
#> 6 6 7 2
#> 7 7 8 2
#> 8 8 9 2
#> 9 9 10 2
#> 10 10 11 2
Created on 2021-04-23 by the reprex package (v0.3.0)