How to convert a matrix of values ​​to a binary matrix

I would like to convert a matrix of values ​​to a matrix of "bits".

I was looking for solutions and found this one that seems to be part of the solution. I will try to explain what I am looking for. I have a matrix like

> x<-matrix(1:20,5,4) > x [,1] [,2] [,3] [,4] [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 13 18 [4,] 4 9 14 19 [5,] 5 10 15 20 

which I would like to convert to

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 2 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 3 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 4 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 5 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 

therefore, for each value in row a "1" in the corresponding column.

If i use

 > table(sequence(length(x)),t(x)) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 5 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 9 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 13 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 14 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 17 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 

this is close to what I'm looking for, but returns a string for each value.

I would only need to combine all the values ​​from one row into one row. Because the

 > table(x) x 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 

gives alls values ​​of the whole table, so what I need to do to get the values ​​for each row.

+7
matrix r
source share
4 answers

Here is another option using the table() function:

 table(row(x), x) # x # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 # 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 # 2 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 # 3 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 # 4 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 # 5 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 
+4
source share
  bit_x = matrix(0, nrow = nrow(x), ncol = max(x)) for (i in 1:nrow(x)) {bit_x[i,x[i,]] = 1} 
+4
source share

Let

 (x <- matrix(c(1, 3), 2, 2)) [,1] [,2] [1,] 1 1 [2,] 3 3 

One approach is

 M <- matrix(0, nrow(x), max(x)) M[cbind(c(row(x)), c(x))] <- 1 M # [,1] [,2] [,3] # [1,] 1 0 0 # [2,] 0 0 1 

In one line:

 replace(matrix(0, nrow(x), max(x)), cbind(c(row(x)), c(x)), 1). 

Following your approach and similar to the @Psidom suggestion:

 table(rep(1:nrow(x), ncol(x)), x) # x # 1 3 # 1 2 0 # 2 0 2 
+4
source share

We can use the reshape2 package.

 library(reshape2) # At first we make the matrix you provided x <- matrix(1:20, 5, 4) # then melt it based on first column da <- melt(x, id.var = 1) # then cast it dat <- dcast(da, Var1 ~ value, fill = 0, fun.aggregate = length) 

what gives us this

  Var1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 1 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 2 2 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 3 3 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 4 4 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 5 5 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 
+3
source share

All Articles