I am trying to produce a frequency table using multiple columns. I have a large dataset, so I need to first select a few columns, convert the data to long format, then present a frequency table of only those who responded yes to the specific questions. Please see data structure below. Please note: 1 = yes and 2 = no.
data:
| subject_id | leq_1_en | leq_2_en | leq_3_en | leq_4_en |
|---|---|---|---|---|
| 191-5467 | 2 | 2 | 1 | 1 |
| 191-6784 | 1 | 1 | 2 | 2 |
| 191-3457 | 1 | 1 | 2 | 2 |
| 191-0987 | 1 | 1 | 2 | 2 |
| 191-1245 | 1 | 1 | 1 | 1 |
| 191-2365 | 1 | 2 | 2 | 1 |
| 191-4589 | 2 | 1 | 1 | 1 |
| 191-9874 | 1 | 1 | 1 | 1 |
| 191-2346 | 2 | 2 | 1 | 1 |
| 191-1256 | 1 | 1 | 1 | 1 |
| 191-6784 | 2 | 1 | 1 | 1 |
| 191-6784 | 1 | 1 | 1 | 1 |
| 191-6784 | 1 | 1 | 1 | 1 |
| 191-6784 | 1 | 1 | 1 | 1 |
Desired output:
| Variable | Frequency |
|---|---|
| leq_1_en | 10 |
| leq_2_en | 11 |
| leq_3_en | 9 |
| leq_4_en | 11 |
What I have tried so far:
data %>% select(leq_1_en, leq_2_en, leq_3_en,leq_4_en) %>%
pivot_longer (.,
names_pattern = "([A-z]+)",
names_to = c("groups")) %>%
drop_na(value) %>%
group_by(groups) %>%
count(value)
output:
Error in `$<-.data.frame`(`*tmp*`, "call_text", value = c("... %>% count(value)", :
replacement has 5 rows, data has 3
I tried to adapt the code in the linked questions below, however, it doesn't work. I am also open to using apply function if that is easier.
Frequency table when there are multiple columns representing one value (R)
Adding a column of total n for each group in a stacked frequency table