我想在预期变量中记录“是”的发生率。
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 Yes in (a* variables)
anymatch <- function(vars){
rowSums(select(cur_data(), all_of(vars))=="Yes")
}
avars <- paste0("a", 1:4)
mydata %>%
mutate(afin = anymatch(avars)) %>%
select(avars, afin)
1条答案
按热度按时间uqzxnwby1#
我们需要
na.rm = TRUE
现在它给出了正确的计数
在将来的版本中,我们可能会使用
pick
而不是cur_data()