dealing with strings of data


I have an item on my survey that respondents could, “choose all that apply”.

Here’s the setup:
Q38. Do have a degree in a science field? 0=No, 1=Yes
Q39. Which of the following best describes your science major? If you had multiple majors, please indicate those. There are 8 choices:

  1. Astronomy
  2. Atmospheric Sciences
  3. Biology
  4. Chemistry
  5. Geology
  6. Ocean sciences
  7. Physical geography
  8. Physics

This created a string of numbers in some cases. I would like to create a new variable that I will call Degree that will have three values:
0. No science degree (picks up the 0 in Q38)

  1. Non-Earth Science degree (combines 3. Biology, 4. Chemistry, and 8. Physics)
  2. Earth Science degree (combines 1. Astronomy, 2. Atmospheric Sciences, 5. Geology, 6. Ocean Sciences, and 7. Physical Geography)

Does anyone have any tricks up your sleeve in accomplishing such a feat?

Hi, possibly something like case_when():

It would be good if you could provide a reproducible example though.