I would like to count the instances where the variables a have been answered (not missing). I tried using pick and rowsums but am unable to find the desired result.
library(dplyr)
anymatch <- function(vars){
rowSums(pick(all_of(is.na(vars))), na.rm = TRUE)
}
data <- tibble(
a1 = c("Yes", NA, "No", "Dont know"),
a2 = c("No", NA, "No", NA),
a3 = c("Good", "Bad", "Good", NA),
b1 = c(20, 34, 23, 45)
)
avars <- data |> select(starts_with("a")) |> names()
data |>
mutate(acount = anymatch(vars))
a data.table
approach
library(data.table)
setDT(data)[rowSums(is.na(data[, .SD, .SDcols = patterns("^a")])) == 0, ]
to create a subset. counting/aggregating is easy from here.
The regex ^a
is used to select the needed columns (name starts with a
).