MATRICES- Creating 3 X 3 IDENTITY MATRIX
Title
Question
I have encountered a problem in creating 3 x 3 identity matrix.
#create a matrix of 3 rows and 3 columns using the following code
values <- c(1,0,0,0,1,0,0,0,1)
matrixB <- as.matrix(values, nrow = 3, ncol = 3, byrow = TRUE)
print(matrixB)
But output i am getting is
print(matrixB)
[,1]
[1,] 1
[2,] 0
[3,] 0
[4,] 0
[5,] 1
[6,] 0
[7,] 0
[8,] 0
[9,] 1
Please tell me the solution..
Thanks.
R Creating-Matrices-using-Data-Frames 06-07 min 30-40 sec
Answers:
Please review your second line of code. You should not use as.matrix function, rather you should use matrix function as given below:
matrixB <- matrix(values, nrow = 3, ncol = 3, byrow = TRUE)
We should not use dot matrix function. Wrong line of code=> <span style="background-color: rgb(250, 250, 250);">matrixB <- as.matrix(values, nrow = 3, ncol = 3, byrow = TRUE) </span>
<span style="background-color: rgb(250, 250, 250);"> Use </span>matrixB<-matrix(values,nrow = 3,ncol = 3, byrow = TRUE)
Hint: as.matrix => used for creating matrix from dataset
<span style="background-color: rgb(250, 250, 250);">
</span>
</span>
<span style="background-color: rgb(250, 250, 250);">
</span>
</span>
codevalues <- c(1,0,0,0,1,0,0,0,1)
matrixB <- matrix(codevalues, nrow = 3, ncol = 3, byrow = TRUE)
print(matrixB)
Login to add comment