vendredi 1 février 2019

Order sets of variables based on unique variable, within rows

I have the following database (db) with the following variables

ID = subjects
Order = order of subjects (1 = ab, 2 = ba) which is the same at all exams

The remaining variables are the weights of each subject (a and b) at different time periods

Wa1 and Wb1 are the weights at the first exam
Wa2 and Wb2 are the weights at the second exam
Wa3 and Wb3 are the weights at the third exam

| ID | Order | Wa1 | Wb1 | Wa2 | Wb2 | Wa3  | Wb3  |
+----+-------+-----+-----+-----+-----+------+------+  
|  1 |     1 | 423 | 252 | NA  | NA  |  234 |  675 |
|  2 |     1 | NA  | NA  | 245 | 856 | 3245 |  423 |
|  3 |     2 | NA  | NA  | NA  | NA  |  534 | 4574 |

I would like to be able to make the database uniform, so that the weights are always listed in the following order: ab (Order == 1)

I have been racking my brain and cannot think of a simple way in R to row-by-row, adjust the order of the weights so that they are all: ab (which is equal to Order == 1)

A very long-handed and time-consuming solution would be to create a new column for each variable (Wa1.1 and Wb1.1, etc. etc.) and use ifelse statements

db$Wa1.1 <- ifelse(db$Order == 2, db$Wb1, db$Wa1)
db$Wb1.1 <- ifelse(db$Order == 2, db$Wa1, db$Wb1)
db$Wa2.1 <- ifelse(db$Order == 2, db$Wb2, db$Wa2)
db$Wb2.1 <- ifelse(db$Order == 2, db$Wa2, db$Wb2)

But this is not practical as the size of my real database is very large and includes many more exams

Any help with a simpler more efficient solution (perhaps using dplyr?) is much appreciated

Aucun commentaire:

Enregistrer un commentaire