What is an efficient way (any welcome solution, including welcome packages) to collapse dummy variables back into a factor.
race.White race.Hispanic race.Black race.Asian 1 1 0 0 0 2 0 0 0 1 3 1 0 0 0 4 0 0 1 0 5 0 0 0 1 6 0 1 0 0 7 1 0 0 0 8 1 0 0 0 9 1 0 0 0 10 0 0 1 0
Required Conclusion:
race 1 White 2 Asian 3 White 4 Black 5 Asian 6 Hispanic 7 White 8 White 9 White 10 Black
Data:
dat <- structure(list(race.White = c(1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L), race.Hispanic = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L), race.Black = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L), race.Asian = c(0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L)), .Names = c("race.White", "race.Hispanic", "race.Black", "race.Asian"), row.names = c(NA, -10L), class = "data.frame")
What I tried:
This is a possible solution, but I'm sure there is a better indexing solution /dplyr/data.table/.etc.
apply(dat, 1, function(x) sub("[^.]+\\.", "", colnames(dat))[x])