Example data:
df <- data.frame(one = c(T, F, F, F, F, T),
two = c(F, F, F, F, T, F),
three = c(F, T, T, T, F, F))
The real data has more columns but this will do as an example. I need a function that turns df
in the following data frame:
data.frame(type = c('one', 'three', 'three', 'three', 'two', 'one'))
I looked at stack and melt and a couple of weird workarounds like the following:
df.new <- data.frame(type = c(rep.int(NA, 6)))
for(i in 1:ncol(df)) {
df.new[,1] <- ifelse(df[,i], colnames(df)[i], df.new[,1])
}
df.new
But is there a function that does this?