I have a dataframe:
tibble{ x = c(1,2,3) y = c(0,2,4) }
I want to add a NEW variable "z" that will be:
z = c("Lower", "Equal", "Higher")
I was thinking about using a for loop but I'm not sure if that's the most efficient/correct way.
The new variable in the dataset can be created with sign after taking the difference of 'x' and 'y', get the sign values, convert it to factor with levels and corresponding labels specified
sign
factor
levels
labels
library(dplyr) df1 %>% mutate(z = factor(sign(x - y), levels = c(-1, 0, 1), c('Lower', "Equal", 'Higher')))
Or an option with case_when
case_when
df1 %>% mutate(tmp = x - y, z = case_when(tmp >0 ~ 'Higher', tmp < 0 ~ 'Lower', TRUE ~ 'Equal'), tmp = NULL)
df1 <- tibble( x = c(1,2,3), y = c(0,2,4))
A base R option
within(df,z <- c("Lower", "Equal", "Higher")[sign(y-x)+2])
which gives
# A tibble: 3 x 3 x y z <dbl> <dbl> <chr> 1 1 0 Lower 2 2 2 Equal 3 3 4 Higher
Copyright © 2021 Jogjafile Inc.
The new variable in the dataset can be created with
sign
after taking the difference of 'x' and 'y', get thesign
values, convert it tofactor
withlevels
and correspondinglabels
specifiedOr an option with
case_when
data