I'm trying to mutate the columns "a" and "b" only if the grouping variable "group" has all observations missing. The attempted (and failed) solution changes the group "blue", in which not all observations are missing. The solution could also involve split() or nest().
Any help is greatly appreciated!
Code below:
library(tidyverse)
# sample data
a <- c(NA,NA,1,1,NA,1)
b <- c(1,1,NA,NA,1,NA)
c <- letters[1:6]
group <- c("yellow","yellow","black","black", "blue", "blue")
(data <- as_tibble(data.frame(a,b,c,group)))
# a b c group
# <dbl> <dbl> <fct> <fct>
# 1 NA 1 a yellow
# 2 NA 1 b yellow
# 3 1 NA c black
# 4 1 NA d black
# 5 NA 1 e blue
# 6 1 NA f blue
# failed attempt: observations from group "blue" change
(data %>%
dplyr::group_by(group) %>%
dplyr::mutate(across(1:2, ~ ifelse(all(is.na(.x)), 99999,.x))))
# a b c group
# <dbl> <dbl> <fct> <fct>
# 1 99999 1 a yellow
# 2 99999 1 b yellow
# 3 1 99999 c black
# 4 1 99999 d black
# 5 NA 1 e blue
# 6 NA 1 f blue
# desired output - observations from blue remain the same
a2 <- c(99999,99999,1,1,NA,1)
b2 <- c(1,1,99999,99999,1,NA)
c2 <- letters[1:6]
group2 <- c("yellow","yellow","black","black", "blue", "blue")
(data_desired <- as_tibble(data.frame(a2,b2,c2,group2)))
# a2 b2 c2 group2
# <dbl> <dbl> <fct> <fct>
# 1 99999 1 a yellow
# 2 99999 1 b yellow
# 3 1 99999 c black
# 4 1 99999 d black
# 5 NA 1 e blue
# 6 1 NA f blue