Creating a Factor from Multiple Categorical Survey Answers

Hey there!
I have a dataframe that consists of 5 integer vectors, with one column representing IDs, and the other 4 vectors that are categorical survey response answers (0= no, 1=yes). The survey questions ask if there is the presence of a variable of interest, with individual questions about each specific subtype (i.e. Column 1 - yes I have this variable of Y variety, Column 2 - Yes I have this variable of Z variety).

Ideally, I would like to create a factor vector, which includes all of the yes responses, with their specific subtype/variety detailed with a labelled level. The end result I am aiming for is the ability to plot a single factor as a bar plot showing all the positive results with a breakdown by the type of subtype.

If there might be any guidance on how to collapse the column categorical vectors into a single factor vector, please let me know and thanks so much!!

To start you off

(in_1 <- data.frame(id=1:5,
           q1=c(0,1,0,1,1),
           q2=c(1,1,0,0,1)))

library(tidyverse)

(step2 <- mutate(in_1,
       across(starts_with("q"),
              ~paste0(cur_column(),"=",.))) )

unite(step2,col =  "all_qs",q1,q2) %>% 
  mutate(all_qs=factor(all_qs))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.