I have a file example.csv contains three columns
A, A, 0
A, B, 0.05
A, C, 0.04
B, A, 0.001
B, B, 0.000
B, C, 0.05
C, A, 0.04
C, B, 0.05
C, C, 0.00
I have defined a 3 x 3 matrix and read the file
mat <- matrix(NA, nrow=3, ncol=3)
df = read.csv(file="~/Desktop/example.csv", head = FALSE, sep=",")
Read the table as matrix
inp.mtx <- as.matrix(df)
inp.mtx
> inp.mtx
V1 V2 V3
[1,] "A" "A" "0.000"
[2,] "A" "B" "0.050"
[3,] "A" "C" "0.040"
[4,] "B" "A" "0.001"
[5,] "B" "B" "0.000"
[6,] "B" "C" "0.050"
[7,] "C" "A" "0.040"
[8,] "C" "B" "0.050"
[9,] "C" "C" "0.000"
mat shows as
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
[3,] NA NA NA
How can I fill first two columns as index and fill the corresponding values? So that the matrix looks like below.
A B C
A 0.000 0.05 0.04
B 0.001 0.000 0.05
C 0.04 0.05 0.00
When I am trying fill I mess up the columns and rows. Sometimes the error as
Error in mat[inp.mtx[, 1:2]] <- inp.mtx[, 3] : subscript out of bounds
Thank you for your inputs!
EDIT: inp.mtx data display