mercredi 29 janvier 2020

Creating new columns based on existing columns in R

This is a sample of my dataframe. It comes from a survey where the original question was: "Where are you located? Mark all that apply."

Code   Option1   Option2   Option3   Option4
101        A        C         NA        NA
102        B        D         NA        NA
103        A        B         D         NA
104        D        NA        NA        NA
105        A        B         C         D

I would like to transform this data so that each column is one of the locations and you get a 0/1 if you're located in any of the 4 locations:

Code   A   B   C   D
101    1   0   1   0
102    0   1   0   1
103    1   1   0   1
104    0   0   0   1
105    1   1   1   1

I tried using ifelse statements, but I kept getting errors. Any suggestions? Thanks!

Aucun commentaire:

Enregistrer un commentaire