【发布时间】:2022-11-22 23:18:31
【问题描述】:
这就是我想要实现的。创建一个我可以重复使用许多变量的函数。
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))
【问题讨论】:
-
简短的回答是的,这是可能的。在这里查看更多信息:dplyr.tidyverse.org/articles/programming.html
-
在 Flickinger 先生的回答中,您还可以将
vars包裹在花括号中,如{{vars}}代替all_of。