Home > Blockchain >  Is there a way to create a function to be used inside mutate isnside %>%
Is there a way to create a function to be used inside mutate isnside %>%

Time:11-23

This is what I would like to achieve. Create a function that I can reuse with many variables.

library(dplyr)

set.seed(2022)
mydata <- tibble::tibble(
  "id" = 1:100,
  "a1" = sample(c(rep("Yes", 40), rep_len(NA, 100)), 100),
  "a2" = sample(c(rep("Yes", 50), rep_len(NA, 100)), 100),
  "a3" = sample(c(rep("Yes", 40), rep_len(NA, 100)), 100),
  "a4" = sample(c(rep("Yes", 50), rep_len(NA, 100)), 100),
  "b2" = rnorm(100, 50, 10)
)

#  Goal is to capture any occurrence of non missing for (a* variables)


avars <- paste0("a", 1:4)

mydata %>%
  mutate(afin = ifelse(rowSums(!is.na(select(., all_of(avars))))>1, "Yes", "No")) %>%
  count(afin)

# Function (Does not work)

anymatch <- function(vars){
  ifelse(rowSums(!is.na(select(., all_of(vars))))>=1, "Yes", "No")
}


mydata %>%
  mutate(afin = anymatch(avars))

CodePudding user response:

If you are always going to be using this function inside of a mutate in a dplyr change, then you can use cur_data() to get the current data.frame rather than .. Actually it's probably safer to always use cur_data() rather than . even when not using a function

anymatch <- function(vars){
  ifelse(rowSums(!is.na(select(cur_data(), all_of(vars))))>=1, "Yes", "No")
}
  • Related