I have have a data frame containing 30 columns in R that contain binary data. Every row contains exactly one 1. In other words the binary values are mutually exclusive. For all the thirty columns no two columns can contain 1 in the same row. Here is an illustration of what I mean.
1 0 0
0 1 0
0 0 1
1 0 0
0 0 1
Now obviously it is computationally enormously expensive to have this information distributed over thirty columns. What I want to do is merge all those 30 columns into one column that contains 30 different factor variables. For example the new column contains 2s in every row where the second column had 1s, 3s in every row where the third column had 1, etc. It is important that the original order is preserved and the positions do not get messed up as they act as index for other columns. So the above 3 columns would become this:
1
2
3
1
3
How can I achieve this in R?
Many thanks
max.col(df)