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))
Hello,
I have a df like this (a very big df). I want to create a new col (A1- which will come before the other cols) and tell R to look at every row, and for any row that has either 4 OR 8 in it (within cols b3 and b8 only), ask R to write YES in col A1 otherwise write NO-. Any advice will be appreciated. Thanks in advance.
You can also use the following solution:
library(dplyr)
library(purrr)
my_df %>%
mutate(A1 = pmap_chr(my_df %>% select(b3: b8), ~ {x <- c(...)[!is.na(c(...))];
c("No", "Yes")[(+any(x %in% c(4, 8)))+1]})) %>%
relocate(A1)
# A tibble: 9 x 11
A1 b1 b2 b3 b4 b5 b6 b7 b8 b9 b10
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 No 2 NA 5 NA 2 9 1 NA 4 14
2 Yes 6 4 9 6 12 2 3 8 5 2
3 Yes 3 6 8 NA 1 4 7 4 7 4
4 No 6 2 NA 10 7 6 7 5 9 2
5 Yes 4 6 2 12 8 7 4 1 5 1
6 Yes 2 6 3 8 5 6 2 4 1 1
7 No 1 1 9 3 5 6 2 1 1 1
8 No 9 1 5 6 6 7 9 3 2 1
9 No NA 7 NA 2 NA 9 5 6 NA 5
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