Convert a nx1 matrix to square diagonal matrix - r

I have a matrix 10x1 matrix a as follows:
[,1]
[1,] 0
[2,] 133
[3,] 206
[4,] 104
[5,] 159
[6,] 0
[7,] 89
[8,] 134
[9,] 0
[10,] 119
I am trying to convert this to a 10x10 diagonal matrix as follows:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 0 0 0 0 0 0 0 0 0
[2,] 0 133 0 0 0 0 0 0 0 0
[3,] 0 0 206 0 0 0 0 0 0 0
[4,] 0 0 0 104 0 0 0 0 0 0
[5,] 0 0 0 0 159 0 0 0 0 0
[6,] 0 0 0 0 0 0 0 0 0 0
[7,] 0 0 0 0 0 0 89 0 0 0
[8,] 0 0 0 0 0 0 0 134 0 0
[9,] 0 0 0 0 0 0 0 0 0 0
[10,] 0 0 0 0 0 0 0 0 0 119
I have tried some basic approaches like diag(a, 10, 10) nothing worked, running out of ideas, any help is much appreciated.

A bit short answer:
diag(as.vector(a))

Related

Number of rows with exactly 1 numerical value in matrice (R)

I have a matrice that is as such:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 0 0 0 0 0 0 0 0 0
[2,] 0 0 0 0 0 0 0 0 0
[3,] 0 0 0 0 0 0 0 0 0
[4,] 0 0 0 0 0 0 0 1 1
[5,] 0 0 0 0 0 0 1 1 0
[6,] 0 0 0 0 0 1 0 0 0
[7,] 0 0 0 0 1 1 0 0 0
[8,] 0 0 0 0 1 0 0 0 0
[9,] 0 0 0 0 1 0 0 0 0
[10,] 0 0 0 0 1 1 0 0 0
[11,] 0 0 0 0 0 1 0 0 0
[12,] 0 0 0 0 0 1 1 1 1
[13,] 0 0 0 0 0 0 0 0 0
[14,] 0 0 0 0 0 0 0 0 0
[15,] 0 0 0 0 0 0 0 0 0
[16,] 0 0 0 0 0 0 0 0 0
[17,] 0 0 0 0 0 0 0 0 0
[,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17]
[1,] 0 0 0 0 0 0 0 0
[2,] 0 0 0 0 0 0 0 0
[3,] 0 0 0 0 0 0 0 0
[4,] 1 1 0 0 0 0 0 0
[5,] 0 1 0 0 0 0 0 0
[6,] 0 1 0 0 0 0 0 0
[7,] 0 1 0 0 0 0 0 0
[8,] 0 1 0 0 0 1 0 0
[9,] 0 1 0 0 0 1 0 0
[10,] 1 1 0 0 0 1 0 0
[11,] 1 1 0 1 1 0 0 0
[12,] 1 1 1 1 0 0 0 0
[13,] 0 0 0 0 0 0 0 0
[14,] 0 0 0 0 0 0 0 0
[15,] 0 0 0 0 0 0 0 0
[16,] 0 0 0 0 0 0 0 0
[17,] 0 0 0 0 0 0 0 0
[,18]
[1,] 0
[2,] 0
[3,] 0
[4,] 0
[5,] 0
[6,] 0
[7,] 0
[8,] 0
[9,] 0
[10,] 0
[11,] 0
[12,] 0
[13,] 0
[14,] 0
[15,] 0
[16,] 0
[17,] 0
How can I count the number of rows with exactly 1 value, not more than one?
I've tried using nrow(imageMatrix[imageMatrix < 2])
and also tried converting the matrice to dataframe and then using nrow(dataframe_matrice[dataframe_matrice == 1,])
but it has been of no avail.
Here imageMatrix is the name of the matrice.
Can someone please offer me a hint on what I'm doing wrong with my first line of code in counting rows?
We may use rowSums on a logical matrix (imageMatrix == 1) and then create a logical vector == 1 and get the count with sum
sum(rowSums(imageMatrix == 1) == 1)
imageMatrix <2 is a logical matrix, when it is used to subset the original matrix, it returns a vector of values which doesn't have dim and thus nrow wouldn't work i.e.
nrow(1:5)
NULL

update cell entries based on a list of X and Y coordinates?

Say that I have a 10 x 5 matrix of zeros in matrix m
m <- matrix(0,10,5)
which looks like this
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 0 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
[6,] 0 0 0 0 0
[7,] 0 0 0 0 0
[8,] 0 0 0 0 0
[9,] 0 0 0 0 0
[10,] 0 0 0 0 0
now I have a list of coordinates in a matrix called xy:
x y
[1,] 3 1
[2,] 7 3
[3,] 8 1
[4,] 9 4
and I want to update the matrix by taking each row of coordinates above and adding 1 to the cell in matrix m that it refers to -- so the output would then look like this
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 1 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
[6,] 0 0 0 0 0
[7,] 0 0 1 0 0
[8,] 1 0 0 0 0
[9,] 0 0 0 1 0
[10,] 0 0 0 0 0
Your help is appreciated!
As long as you provide the coordinates as a matrix, 1st column specifiying row, 2nd column specifiying column, you can do:
xy = cbind(c(3,7,8,9),c(1,3,1,4))
m[xy] = 1
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 1 0 0 0 0
[4,] 0 0 0 0 0
[5,] 0 0 0 0 0
[6,] 0 0 0 0 0
[7,] 0 0 1 0 0
[8,] 1 0 0 0 0
[9,] 0 0 0 1 0
[10,] 0 0 0 0 0

r apply function to calculate proportions by row in a matrix

I'm using apply function to calculate migration transition probabilities from eight states at time t1 to the same eight states at time t2. My data is save in matrix format and named tmp (as follows). the states at time t1 are my rows, and the states at time t2 are my columns. e.g. 228 persons stayed at state 1 between t1 and t2; 3 persons moved from state 2 to state 1 between t1 and t2.
> class(tmp)
"matrix"
> tmp
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 228 0 0 0 0 0 0 0
[2,] 3 92 0 0 0 0 0 0
[3,] 0 0 30 0 0 0 0 0
[4,] 0 0 0 20 0 0 0 0
[5,] 0 0 0 0 19 0 0 0
[6,] 0 0 0 0 0 0 0 0
[7,] 0 0 0 3 0 0 0 0
[8,] 0 0 0 0 0 0 0 3
I used the follow code to calculate the probabilities of moving to or staying in a certain state. It is the proportions for cells in each row. The results are saved in tmp1.
> tmp1=apply(tmp,1,function(X){if (sum(X)!=0) {X/sum(X)} else {numeric(length(X))}})
The problem is: I expected tmp1[4,7] to be 0 (because tmp[4,7] is 0), but the code returns me 1 (bolded). Is there a problem in my apply function?
> tmp1
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 0.03157895 0 0 0 0 0 0
[2,] 0 0.96842105 0 0 0 0 0 0
[3,] 0 0.00000000 1 0 0 0 0 0
[4,] 0 0.00000000 0 1 0 0 **1** 0
[5,] 0 0.00000000 0 0 1 0 0 0
[6,] 0 0.00000000 0 0 0 0 0 0
[7,] 0 0.00000000 0 0 0 0 0 0
[8,] 0 0.00000000 0 0 0 0 0 1

Place a vector randomly inside a matrix in R

How can I place the vector a<-c(1,2,3,4,5,6) in a ramdom position in the matrix m<-matrix(0, nrow = 10, ncol = 10)?
The vector has to be together:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 0 0 0 0 0 0 0 0 0
[2,] 1 0 0 0 0 0 0 0 0 0
[3,] 2 0 0 0 0 0 0 0 0 0
[4,] 3 0 0 0 0 0 0 0 0 0
[5,] 4 0 0 0 0 0 0 0 0 0
[6,] 5 0 0 0 0 0 0 0 0 0
[7,] 6 0 0 0 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
And it has to be horizontally, vertically or diagonally
I have tried:
start = sample.int(length(m), 1)
m[start:(start+length(a)-1)] = a
But it cannot take place the following:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 0 0 0 0 0 5 0 0 0
[2,] 0 0 0 0 0 0 6 0 0 0
[3,] 0 0 0 0 0 0 0 0 0 0
[4,] 0 0 0 0 0 0 0 0 0 0
[5,] 0 0 0 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 0 0 0 0 0
[7,] 0 0 0 0 0 1 0 0 0 0
[8,] 0 0 0 0 0 2 0 0 0 0
[9,] 0 0 0 0 0 3 0 0 0 0
[10,] 0 0 0 0 0 4 0 0 0 0
Thanks
Sample a 1d index of the same size as a and then assign the vector to m at those indices:
m[sample.int(length(m), length(a))] <- a
m
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] 0 6 0 0 0 0 0 0 0 0
# [2,] 0 0 0 0 0 4 0 0 0 0
# [3,] 0 3 0 1 0 0 0 0 5 0
# [4,] 0 0 0 0 0 0 0 0 0 0
# [5,] 0 0 0 0 0 0 0 0 0 0
# [6,] 0 0 0 0 0 0 0 0 0 0
# [7,] 0 0 0 0 0 0 0 0 0 0
# [8,] 0 0 0 0 0 0 0 0 2 0
# [9,] 0 0 0 0 0 0 0 0 0 0
#[10,] 0 0 0 0 0 0 0 0 0 0
If the vector needs to be continuous, you can sample the start index, and then assign with range index:
start = sample.int(length(m), 1)
m[start:(start+length(a)-1)] = a
m
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] 0 0 0 0 0 0 0 0 0 0
# [2,] 0 0 0 0 0 0 0 0 0 0
# [3,] 0 0 0 0 0 0 0 0 0 0
# [4,] 0 0 0 0 0 0 0 0 0 0
# [5,] 1 0 0 0 0 0 0 0 0 0
# [6,] 2 0 0 0 0 0 0 0 0 0
# [7,] 3 0 0 0 0 0 0 0 0 0
# [8,] 4 0 0 0 0 0 0 0 0 0
# [9,] 5 0 0 0 0 0 0 0 0 0
#[10,] 6 0 0 0 0 0 0 0 0 0

How to iteratively fill a matrix with rnorm values using a for loop?

I have a question, I am trying to create a 10x10 matrix using the code below, where the first column contains 10 values from a normal distribution with std dev of .5 and a mean equal to j where j is a value 1:10. My code below produces the observed matrix, where only the final column is filled with values. What am I doing wrong? Thank you.
for(j in 1:10){
y<-matrix(0,ncol=10,nrow=10)
y[,j]<-rnorm(n=10,mean=j,sd=.5)
}
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 0 0 0 0 0 0 0 0 0 10.857520
[2,] 0 0 0 0 0 0 0 0 0 10.490549
[3,] 0 0 0 0 0 0 0 0 0 9.888620
[4,] 0 0 0 0 0 0 0 0 0 9.495205
[5,] 0 0 0 0 0 0 0 0 0 9.674356
[6,] 0 0 0 0 0 0 0 0 0 10.810197
[7,] 0 0 0 0 0 0 0 0 0 10.337517
[8,] 0 0 0 0 0 0 0 0 0 9.715229
[9,] 0 0 0 0 0 0 0 0 0 9.902603
[10,] 0 0 0 0 0 0 0 0 0 8.972656

Resources