mardi 30 avril 2019

Sorting data into groups

I have a data set where participants had to choose 3 out of 20 people (e.g. W26 means the person they chose is a 26-year old woman.) This means, that if they chose W26, M20 and W30 these variables each have the code "1" in my data, and the other 17 people get the code "0". Now I have about 200 participants that each chose 3 people. Now I want to sort the list with the following structure so that it is easy to see who chose who and what gender and age they are. This is the sort of list I want:

                  person1_gender person1_age    person2_gender  person2_age        
  participant 1   female         26             male           20               
  participant 2   male           32             female         19
  participant 3   male           19             male           30

etc.

I have tried to do this with the if code ... which means that if the code = 1 then they should say "female" or "male" respectively, but I am actually not sure at all if I have to do other things before I use the if code.

 if (data$M18 > 0) 

   data$M18 = "male"

   print (data$M18)

The output does give me the following: "Warning message: In if (data$M18 > 0) data$M18 = "male" : the condition has length > 1 and only the first element will be used"

And the data also does not change to "male" at all. Do I need to work with loops as well?

Aucun commentaire:

Enregistrer un commentaire