Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert a matrix of values into a binary matrix

Tags:

r

matrix

I'd like to convert a matrix of values into a matrix of 'bits'.

I have been looking for solutions and found this, which seems to be part of a solution. I'll 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 into

     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

so for each value in the 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 am looking for, but returns a line for each value.

I would only need to consolidate all values from one row into one row. Because a

> 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 do I need to do to get the values per row.

like image 651
Kurt Ludikovsky Avatar asked Jul 17 '16 15:07

Kurt Ludikovsky


Video Answer


1 Answers

Here is another option using 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
like image 122
Psidom Avatar answered Oct 22 '22 03:10

Psidom