I have a large dataset that includes several variables named: F12.4.1 … F12.4.10.
Each of them has the same pattern, where 1 means yes, and 0 means no.
I want to mutate these variables into one new variable that only includes yes, so it would look like this:
F12.4.1 — 30
F12.4.2 — 20
etc.
I have created this code:
library(dplyr)
homeschooling <- data %>%
rowwise() %>%
mutate(yes_count = sum(c_across(c(`F12.4.1`, `F12.4.2`, `F12.4.3`, `F12.4.4`, `F12.4.5`, `F12.4.6`, `F12.4.7`, `F12.4.8`, `F12.4.9`, `F12.4.10`))))
But the end result is for some reason wrong. It lists all the variables but they don’t have the correct numbers.
—
Data
set.seed(2024)
data <- replicate(10L, rbinom(20L, 1, 1/2)) |>
as.data.frame() |>
setNames(paste0("F12.4.", 1:10))