I'd like to pass a list of variables to a function, but I'm confused by the quoting and quosures.
Normally, I want to return a df after some kind of data management has been done--after the function has been applied to several variables.
As it is the function works ok (just slightly modified from a users stack exchange answer to another question), but the calls are repetitive in this example. Any suggestions, points to readings or etc., I'd appreciate.
library(tidyverse)
library(rlang)
library(tidyselect)
data <- data.frame(ageeeeoo = c(1,NA,3,NA,5),
ageeeaah = c(NA,2,NA,4,NA),
numnumd = c(1,NA,3,NA,5),
numfoofe = c(NA,2,NA,4,NA))
newfun <- function (var1) {
var1<-enquo(var1)
data<<-mutate(data,(!!as_name(var1)) := coalesce(!!! syms(vars_select(names(data),
starts_with(as_name(var1))))))
}
newfun(age)
newfun(num)
ageeeeoo ageeeaah numnumd numfoofe age num
1 NA 1 NA 1 1
NA 2 NA 2 2 2
3 NA 3 NA 3 3
NA 4 NA 4 4 4
5 NA 5 NA 5 5
I tried reviewing the dplyr programming documents and a few other stack exchange QA but the quoting throws me off. I've tried using alist and list but get errors.
listofvars<-c("age","num")
newfun <- function (...) {
data<<-mutate(data,(!!!rlang::syms(...)) := coalesce(!!! syms(vars_select(names(data),
starts_with(!!!quos(...))))))
}
newfun(listofvars)