my_df <- tibble(
b1 = c(2, 6, 3, 6, 4, 2, 1, 9, NA),
b2 = c(NA, 4, 6, 2, 6, 6, 1, 1, 7),
b3 = c(5, 9, 8, NA, 2, 3, 9, 5, NA),
b4 = c(NA, 6, NA, 10, 12, 8, 3, 6, 2),
b5 = c(2, 12, 1, 7, 8, 5, 5, 6, NA),
b6 = c(9, 2, 4, 6, 7, 6, 6, 7, 9),
b7 = c(1, 3, 7, 7, 4, 2, 2, 9, 5),
b8 = c(NA, 8, 4, 5, 1, 4, 1, 3, 6),
b9 = c(4, 5, 7, 9, 5, 1, 1, 2, NA),
b10 = c(14, 2, 4, 2, 1, 1, 1, 1, 5))
Hi Guys,
Hope you are all good. I have a df like this (very big one), and I want to tell R to add 10 to the values in b1 if there is 2 in either b6, 67, b8 or b9. Thanks once again in anticipation.
We can create a logical condition in case_when by taking the row sums of subset of columns b6:b9 to find if the row have at least 2 in any of the row then add 10 to b1 or else return the original column
library(dplyr)
my_df <- my_df %>%
mutate(b1 = case_when(rowSums(select(cur_data(), b6:b9) == 2,
na.rm = TRUE) > 0 ~ b1 + 10, TRUE ~ b1))
-output
my_df
# A tibble: 9 x 10
b1 b2 b3 b4 b5 b6 b7 b8 b9 b10
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2 NA 5 NA 2 9 1 NA 4 14
2 16 4 9 6 12 2 3 8 5 2
3 3 6 8 NA 1 4 7 4 7 4
4 6 2 NA 10 7 6 7 5 9 2
5 4 6 2 12 8 7 4 1 5 1
6 12 6 3 8 5 6 2 4 1 1
7 11 1 9 3 5 6 2 1 1 1
8 19 1 5 6 6 7 9 3 2 1
9 NA 7 NA 2 NA 9 5 6 NA 5
Or may also use if_any
my_df %>%
mutate(b1 = case_when(if_any(b6:b9, `%in%`, 2) ~ b1 + 10, TRUE ~ b1))
-output
# A tibble: 9 x 10
b1 b2 b3 b4 b5 b6 b7 b8 b9 b10
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2 NA 5 NA 2 9 1 NA 4 14
2 16 4 9 6 12 2 3 8 5 2
3 3 6 8 NA 1 4 7 4 7 4
4 6 2 NA 10 7 6 7 5 9 2
5 4 6 2 12 8 7 4 1 5 1
6 12 6 3 8 5 6 2 4 1 1
7 11 1 9 3 5 6 2 1 1 1
8 19 1 5 6 6 7 9 3 2 1
9 NA 7 NA 2 NA 9 5 6 NA 5
Or the same in base R
i1 <- rowSums(my_df[6:9] == 2, na.rm = TRUE) > 0
my_df$b1[i1] <- my_df$b1[i1] + 10
Or with Reduce/lapply and %in%
i1 <- Reduce(`|`, lapply(my_df[6:9], `%in%`, 2))
my_df$b1[i1] <- my_df$b1[i1] + 10
You can also use the following solution:
library(dplyr)
library(purrr)
my_df %>%
pmap_df(~ {x <- c(...)[6:9];
y <- c(...)[1]
if(any(2 %in% x[!is.na(x)])) {
y + 10
} else {
y
}
}) %>%
bind_cols(my_df[-1])
# A tibble: 9 x 10
b1 b2 b3 b4 b5 b6 b7 b8 b9 b10
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 2 NA 5 NA 2 9 1 NA 4 14
2 16 4 9 6 12 2 3 8 5 2
3 3 6 8 NA 1 4 7 4 7 4
4 6 2 NA 10 7 6 7 5 9 2
5 4 6 2 12 8 7 4 1 5 1
6 12 6 3 8 5 6 2 4 1 1
7 11 1 9 3 5 6 2 1 1 1
8 19 1 5 6 6 7 9 3 2 1
9 NA 7 NA 2 NA 9 5 6 NA 5
Or we can use this thanks to a great suggestion by dear @akrun:
my_df %>%
mutate(b1 = ifelse(pmap_lgl(select(cur_data(), b6:b9), ~ 2 %in% c(...)), b1 + 10, b1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With