I would like to transform multiple binary columns into one column with categorical content based on the rownames of the binary columns. I have tried to use the code in this forum in other questions but as my case differs in that some rows have no entry, I have not managed to find an efficient solution. The dplyr
solution proposed there is slow on my dataset. My dataset has over 2 Mio rows.
Here is the data
m <- matrix(0,10,10)
colnames(m) <- c("a","b","c","d","e","f","g","h","i","j")
m[3,2] <- 1
m[4,8] <- 1
m[5,8] <- 1
m[6,1] <- 1
looking like this
a b c d e f g h i j
[1,] 0 0 0 0 0 0 0 0 0 0
[2,] 0 0 0 0 0 0 0 0 0 0
[3,] 0 1 0 0 0 0 0 0 0 0
[4,] 0 0 0 0 0 0 0 1 0 0
[5,] 0 0 0 0 0 0 0 1 0 0
[6,] 1 0 0 0 0 0 0 0 0 0
[7,] 0 0 0 1 0 0 0 0 0 0
[8,] 0 0 0 0 0 0 0 0 0 0
[9,] 0 0 0 0 0 0 0 0 0 0
[10,] 0 0 0 0 0 0 0 0 0 0
I would like to get
colname
[1,] ""
[2,] ""
[3,] "b"
[4,] "h"
[5,] "h"
[6,] "a"
[7,] "d"
[8,] ""
[9,] ""
[10,] ""