# Using mutate and applying logic based on column walues

Just looking for some guidance on the best way to approach the logic for the result I am trying to achieve.

I have a dataset with a number of responses to multiple choice questions, with the answers spread across columns. I am using mutate to create a new column with a summarise/calculated value.

The issue I have is that the question was a multiple select, and using the code as I have below, is if someone has selected the first option, then there is no consideration for the other options they have chosen.

ie if I choose 1, and 3, the below code will only return 1

``````#this option is conditional based on which questions
df <- df %>%
mutate(participation = case_when(
q0040_0001 == 1 ~ 1, #1 = Casual
q0040_0002 == 1 ~ 2, #2 = regular
q0040_0003 == 1 ~ 3, #3 = advanced/elite
q0040_0004 == 1 ~ 4, #4 =in the future
q0040_0005 == 1 ~ 5, #5 = used to

``````

I have looked at writing out the code like this, but there would be appx 30 lines of code to cater for all the variations and I feel there must be a smarter/better way of doing this. It would also grow considerably for questions that have more than 5 options.

``````q0041_0001 == 1 & q0041_0002 == 1 & q0041_0003 != 1 & q0041_0004 != 1 & q0041_0005 != 1 ~ 2, # 1 & 2
q0041_0001 == 1 & q0041_0002 == 1 & q0041_0003 == 1 & q0041_0004 != 1 & q0041_0005 != 1 ~ 4, # 1 & 2 & 3
q0041_0001 == 1 & q0041_0002 == 1 & q0041_0003 == 1 & q0041_0004 == 1 & q0041_0005 != 1 ~ 5, # 1 & 2 & 3 & 4
``````

This is an example of a subset of the data, ie this is question 41, with the 5 options.

``````df <- data.frame(
q0041_0001 = c(1, 1, 0, 1, 0),
q0041_0002 = c(0, 1, 0, 0, 1),
q0041_0003 = c(0, 1, 0, 0, 1),
q0041_0004 = c(1, 0, 1, 1, 0),
q0041_0005 = c(0, 1, 1, 0, 1),
)
``````

Hello.
Thanks for providing code , but you could take further steps to make it more convenient for other forum users to help you.

Share some representative data that will enable your code to run and show the problematic behaviour.

You might use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

Reprex Guide

1 Like

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.