Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading