Reputation: 416
I am looking to apply a mutate only when certain conditions are met.
I know I can do this...
data2 <- data1 %>%
group_by(a, b) %>%
mutate(
var1 = case_when(
condition ~ TRUE,
TRUE ~ FALSE,
NA
),
var2 = case_when(
condition ~ TRUE,
max(var28),
var2
),
var3 = case_when(
condition ~ TRUE,
"happy",
var3
),
...more vars here....
)
What I would like is something that looks like this...
data2 <- data1 %>%
group_by(a, b) %>%
mutate(
when(condition),
var1 = FALSE,
var2 = max(var28),
var3 = "happy",
...more vars here....
)
Unfortunately mutate(across(when(condition)))
did not work.
Any suggesions?
Upvotes: 2
Views: 1922
Reputation: 26505
Another potential solution is to use across() and ifelse(), e.g. if the value in column x is less than four, perform the mutation:
library(tidyverse)
tibble(x = c(1:2, 4:5), y = 1:4) %>%
mutate(across(everything(), ~ ifelse(x < 4, -.x, .x)))
#> # A tibble: 4 × 2
#> x y
#> <int> <int>
#> 1 -1 -1
#> 2 -2 -2
#> 3 4 3
#> 4 5 4
Created on 2021-11-24 by the reprex package (v2.0.1)
You can also nest ifelse's together, e.g. if the value in column x is less than 4, or if a value in any column is equal to 3, mutate:
library(tidyverse)
tibble(x = c(1:2, 4:5), y = 1:4) %>%
mutate(across(everything(), ~ ifelse(x < 4, -.x,
ifelse(.x == 3, .x + 10, .x))))
#> # A tibble: 4 × 2
#> x y
#> <int> <dbl>
#> 1 -1 -1
#> 2 -2 -2
#> 3 4 13
#> 4 5 4
Created on 2021-11-24 by the reprex package (v2.0.1)
And, so on:
library(tidyverse)
tibble(x = c(1:2, 4:5), y = 1:4) %>%
mutate(across(everything(), ~ ifelse(x < 4, -.x,
ifelse(.x == 3, .x + 10,
ifelse(.x >= 5, "outlier", .x)))))
#> # A tibble: 4 × 2
#> x y
#> <chr> <dbl>
#> 1 -1 -1
#> 2 -2 -2
#> 3 4 13
#> 4 outlier 4
Created on 2021-11-24 by the reprex package (v2.0.1)
--
To do the mutation more efficiently, don't use dplyr::mutate. The ifelse() function is vectorized (more details: https://swcarpentry.github.io/r-novice-gapminder/09-vectorization/) and if you have a large dataframe, ifelse alone will very likely be faster than tidyverse functions, e.g.
library(tidyverse)
df <- tibble(x = c(1:2, 4:5), y = 1:4)
df$y <- ifelse(df$x < 4, -df$y, df$y)
df
#> # A tibble: 4 × 2
#> x y
#> <int> <int>
#> 1 1 -1
#> 2 2 -2
#> 3 4 3
#> 4 5 4
Another potential option is to replace the values via assignment: df$y[df$x < 4] <- -(df$y); df$x[df$x < 4] <- -(df$x)
(fast, but there are limitations).
Here is a quick benchmark of the suggested methods with 1 million rows:
library(tidyverse)
df <- tibble(x = sample(1:10, 1000000, replace = TRUE),
y = sample(1:10, 1000000, replace = TRUE))
mutate_func <- function(df){
df %>%
mutate(across(everything(), ~ ifelse(x < 4, -.x, .x)))
}
ifelse_func <- function(df){
df$y <- ifelse(df$x < 4, -df$y, df$y)
}
replacement_func <- function(df) {
df$y[df$x < 4] <- -(df$y)
df$x[df$x < 4] <- -(df$x)
}
mutate_when_func <- function(df) {
mutate_when <- function(.data, when, ...) {
dots <- enquos(...)
names <- names(dots)
mutate(.data, {
test <- {{ when }}
changed <- data.frame(!!!dots, stringsAsFactors = FALSE)
out <- across(all_of(names))
# assuming `changed` and `out` have the same data frame type
out[test, ] <- changed[test, ]
out
})
}
df %>%
mutate_when(x < 4, x = -x, y = -y)
}
library(microbenchmark)
result <- microbenchmark(mutate_func(df), ifelse_func(df),
mutate_when_func(df), replacement_func(df),
times = 10)
autoplot(result)
Upvotes: 5
Reputation: 28675
There is no functionality to do this in mutate, but Romain Francois has shared a function you could define yourself which does this:
library(dplyr, warn.conflicts = F)
mutate_when <- function(.data, when, ...) {
dots <- enquos(...)
names <- names(dots)
mutate(.data, {
test <- {{ when }}
changed <- data.frame(!!!dots, stringsAsFactors = FALSE)
out <- across(all_of(names))
# assuming `changed` and `out` have the same data frame type
out[test, ] <- changed[test, ]
out
})
}
tibble(x = 1:4, y = 1:4) %>%
mutate_when(x < 4, x = -x, y = -y)
#> # A tibble: 4 × 2
#> x y
#> <int> <int>
#> 1 -1 -1
#> 2 -2 -2
#> 3 -3 -3
#> 4 4 4
Created on 2021-11-23 by the reprex package (v2.0.1)
Upvotes: 3