I dont know any other good way to express the title.
I have one hot encoded a given matrix. Example is here
> set.seed(4)
> t <- matrix(floor(runif(10, 1,9)),5,5)
[,1] [,2] [,3] [,4] [,5]
[1,] 5 3 5 3 5
[2,] 1 6 1 6 1
[3,] 3 8 3 8 3
[4,] 3 8 3 8 3
[5,] 7 1 7 1 7
> class(t)
[1] "matrix"
1_1 1_3 1_5 1_7 2_1 2_3 2_6 2_8 3_1 3_3 3_5 3_7 4_1 4_3 4_6 4_8 5_1 5_3 5_5 5_7
[1,] 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 1 0
[2,] 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0
[3,] 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0
[4,] 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0
[5,] 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1
I have been strugling to transform another matrix for example as below to transform into expected form.
[,1] [,2] [,3] [,4] [,5]
[1,] 7 4 8 1 3
[2,] 3 7 4 8 1
[3,] 1 3 7 4 8
[4,] 8 1 3 7 4
Expecting the following, where the column remains as the previous encoded matrix, but the columns need to be filled with 0s
and 1s
according the vlaues in the new given matrix.
1_1 1_3 1_5 1_7 2_1 2_3 2_6 2_8 3_1 3_3 3_5 3_7 4_1 4_3 4_6 4_8 5_1 5_3 5_5 5_7
[1,] 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0
[2,] 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0
[3,] 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0
[4,] 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
Since the column names are different to the values in new given matrix, i do not know how to check new given column values to old column values.
Any suggestion or hint would help a big time. I struggled with this the whole weekend.