I am trying to mutate multiple columns in a data frame based on several conditions as follows.
if a value >= 0.25, value = 1
value <= 0.25, value = 1
value <= 1.2, value = 2
value >= -1.2, value = -2
value <= 0.25 & value >=-0.25, value = 0
Data:
structure(list(col = c("a", "b", "c", "d", "e"), col2 = c(0.023,
0.41, 0.032, -0.58, -2.6), col3 = c(-0.123, 0.551, -0.032, 0.58,
-4.6), col4 = c(-0.23, 0.1, 0.25, 1.858, -6.6)), class = "data.frame", row.names = c(NA,
-5L))
I have more than 75 columns so I couldn't use the traditional case_when with a single column. I really hope someone could help.
CodePudding user response:
A possible solution:
library(tidyverse)
df <- structure(list(col = c("a", "b", "c", "d", "e"), col2 = c(0.023, 0.41, 0.032, -0.58, -2.6), col3 = c(-0.123, 0.551, -0.032, 0.58, -4.6), col4 = c(-0.23, 0.1, 0.25, 1.858, -6.6)), class = "data.frame", row.names = c(NA, -5L))
df %>%
mutate(across(matches("\\d$"), ~ case_when(.x >= 0.25 ~ 1,
.x <= 0.25 ~ 1,
.x <= 1.2 ~ 2,
.x >= -1.2 ~ -2,
.x <= 0.25 & .x >=-0.25 ~ 0)))
#> col col2 col3 col4
#> 1 a 1 1 1
#> 2 b 1 1 1
#> 3 c 1 1 1
#> 4 d 1 1 1
#> 5 e 1 1 1
CodePudding user response:
We could use mutate
and across
: The difference to @PaulS 1 is to exclude the first column and using .names
argument.
library(dplyr)
df %>%
mutate(across(-col, ~case_when(
. >= 0.25 ~ 1,
. <= 0.25 ~ 1,
. <= 1.2 ~ 2,
. >= -1.2 ~ -2,
. <= 0.25 & . >=-0.25 ~ 0
), .names = "new_{.col}"))
col col2 col3 col4 new_col2 new_col3 new_col4
1 a 0.023 -0.123 -0.230 1 1 1
2 b 0.410 0.551 0.100 1 1 1
3 c 0.032 -0.032 0.250 1 1 1
4 d -0.580 0.580 1.858 1 1 1
5 e -2.600 -4.600 -6.600 1 1 1