I can't believe I'm struggling with this after just 3 years of taking a break from R...
Basically, I'm having a dataframe where in the first column all possible values are listed. The subsequent columns have either one of the values or NA. I would love to sort every subsequent column in a way so that when it contains the value at some point, it shall be in the row where that value is in the first column.
Probably easier to explain with an example:
Original:
Letters Category1 Category2 Category3 Category4
A NA A NA NA
B A NA NA D
C NA NA NA A
D NA C NA NA
E E B C NA
Desired state:
Letters Category1 Category2 Category3 Category4
A A A NA A
B NA B NA NA
C NA C C NA
D NA NA NA D
E E NA NA NA
Am I overlooking a built-in function / library to do this elegantly? My approach would probably to build a function that creates a new dataframe and checks the contents row by row, but this seems very inefficient...