One way to fill in the matrix in the correct order is by using the matrix transpose.
Create some data: lower triangular correlation matrix output
m <- cor(mtcars[1:4])
m[upper.tri(m)] <- NA
write.table(m, temp<-tempfile(), row.names = FALSE, col.names=FALSE, na="")
Read in using standard tools
# Change `temp` to the path to your matrix file
inmat <- as.matrix(read.table(temp, fill=TRUE))
# Fill in upper triangle
inmatt <- t(inmat)
inmatt[lower.tri(inmatt, diag=FALSE)] <- inmat[lower.tri(inmat, diag=FALSE)]
Check
all.equal(cor(mtcars[1:4]), inmatt, check.attributes=FALSE)
Okay as you are reading from an image, to save manually typing you may be able to read the text from the image.
library(tesseract)
# Read image
engine <- tesseract(options = list(tessedit_char_whitelist = ".-0123456789"))
text <- ocr("https://i.stack.imgur.com/OHf3z.jpg", engine)
cat(text) # looks okay
cat(text, file=temp<-tempfile())
# Now try to fill matrix
v = as.numeric(unlist(strsplit(readLines(temp), " ")))
inmat <- diag(15) # just hard-code matrix dimension
inmat[upper.tri(inmat, diag=TRUE)] <- v
inmatt <- t(inmat)
inmatt[upper.tri(inmatt, diag=TRUE)] <- v
To produce
inmatt
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,] 1.00 0.77 0.53 0.54 0.54 0.30 0.16 0.36 -0.11 -0.10 -0.02 0.14 0.09 0.21 0.16
[2,] 0.77 1.00 0.50 0.44 0.48 0.28 0.20 0.34 -0.05 -0.09 -0.07 -0.02 -0.01 0.18 0.21
[3,] 0.53 0.50 1.00 0.74 0.91 0.72 0.28 0.79 0.08 -0.03 0.22 0.04 0.06 0.15 0.22
[4,] 0.54 0.44 0.74 1.00 0.82 0.63 0.19 0.56 0.02 -0.07 0.23 0.05 0.10 0.15 0.22
[5,] 0.54 0.48 0.91 0.82 1.00 0.75 0.26 0.70 0.05 -0.08 0.21 0.07 0.09 0.14 0.22
[6,] 0.30 0.28 0.72 0.63 0.75 1.00 0.31 0.63 0.20 0.02 0.27 -0.03 -0.03 -0.01 0.23
[7,] 0.16 0.20 0.28 0.19 0.26 0.31 1.00 0.38 0.03 0.15 0.29 0.23 0.24 0.16 0.29
[8,] 0.36 0.34 0.79 0.56 0.70 0.63 0.38 1.00 0.14 0.05 0.37 0.11 0.11 0.13 0.18
[9,] -0.11 -0.05 0.08 0.02 0.05 0.20 0.03 0.14 1.00 0.50 0.44 -0.04 -0.10 0.13 0.03
[10,] -0.10 -0.09 -0.03 -0.07 -0.08 0.02 0.15 0.05 0.50 1.00 0.62 0.37 0.08 0.11 -0.07
[11,] -0.02 -0.07 0.22 0.23 0.21 0.27 0.29 0.37 0.44 0.62 1.00 0.31 0.21 0.28 0.09
[12,] 0.14 -0.02 0.04 0.05 0.07 -0.03 0.23 0.11 -0.04 0.37 0.31 1.00 0.73 0.12 0.10
[13,] 0.09 -0.01 0.06 0.10 0.09 -0.03 0.24 0.11 -0.10 0.08 0.21 0.73 1.00 0.31 0.32
[14,] 0.21 0.18 0.15 0.15 0.14 -0.01 0.16 0.13 0.13 0.11 0.28 0.12 0.31 1.00 0.41
[15,] 0.16 0.21 0.22 0.22 0.22 0.23 0.29 0.18 0.03 -0.07 0.09 0.10 0.32 0.41 1.00
Another option is to pass an image or url of an image to http://www.free-ocr.com/ which will try to parse. It will create a text doc of the image which you can then read in.
matrix()
requires desired numbers of rows (nrow
) and columns (ncol
) but does not assume "empty" cells. – nghauran