Assign 1 to the matrix from the coordinate list

I want to take a list of coordinates. For each location in coordinates, I want to assign 1 matrix. The final matrix should look like this. I want to use a fast vector method instead of a for loop.

> sample.matrix ABCDEFGHIJKLMNOPQRST A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 B 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 C 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 D 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 E 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 F 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 G 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 H 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 I 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 J 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 K 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 L 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 N 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 O 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 P 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 Q 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 R 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 S 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 T 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

Sample data:

  set.seed(15) dat <- data.frame(x=sample(LETTERS[1:20], 15,set.seed(15)),y=sample(LETTERS[1:20], 15,set.seed(15)), stringsAsFactors = FALSE) sample.matrix <- matrix(data=0, nrow = 20, ncol = 20, dimnames = list(LETTERS[1:20],LETTERS[1:20])) 

What I tried worked.

  for ( n in 1:15){ sample.matrix[dat[n,]$x,dat[n,]$y] =1 } 

What I tried does not work.

 by(dat, 1:15, function(a.row){sample.matrix[a.row$x, a.row$y]=1}) 
+7
vectorization r
source share
3 answers

This is a cool programming aspect of the language. In R, we can subset using two column matrices:

 sample.matrix[as.matrix(dat)] <- 1 

Learn more about how it works.

From the help for ?Extract (bold text is my accent):

The third form of indexing is through a numerical matrix with one column for each dimension : each row of the index matrix selects a single element of the array, and the result is a vector. negative indices are not allowed in the index matrix. NA and null values ​​are allowed: rows of the index matrix containing zero are ignored, while rows containing NA form the result NA.

Indexing through a matrix of characters with one column per dimension is also supported if the array has dimension names . As with numeric matrix indexing, each row of the index matrix selects one element of the array. Indexes are mapped to the corresponding sized names. NA is permitted and will produce NA as a result. Peerless indexes as well as an empty string ("") are not allowed and will result in an error.

In your case, you have a matrix with letters that represent the names of the dimensions.

  xy [1,] "B" "C" [2,] "S" "S" [3,] "L" "B" [4,] "N" "L" [5,] "E" "G" [6,] "M" "A" 

When we insert this matrix into the bracket set [as.matrix(dat)] , the evaluator will detect the matrix and use a special type of subset. The first column will represent the rows, and the second column will represent the columns.

 head(sample.matrix) ABCDEFGHIJKLMNOPQRST A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 B 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 C 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 D 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 E 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 F 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 
+8
source share

You can use dcast from data.table :

 dat$z <- 1 dcast(dat,x~y,fill=0, value="z") x BCDGHIJKLMNOPQT 1 A 0 0 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0 0 0 0 0 0 0 0 0 K 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 L 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 M 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 N 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 O 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 P 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 Q 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 R 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 S 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 T 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
+1
source share

All Articles