To convert vector
to matrix
, you can simply use the matrix
function:
matrix(data, nrow, ncol)
Depending if the data should be populated by rows instead of by columns, you can specify the parameter byrow=TRUE
In your particular case, read the file with readLines
:
pgm = readLines("1_1.pgm")
For other users that do not have access to your file, here is output from dput
that can be simply copypasted into R console:
pgm = c("P2", "# Created by GIMP version 2.10.22 PNM plug-in", "25 25",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "2", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "2", "2", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "2", "2", "2", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "2", "2", "255", "2", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "2", "2", "255",
"255", "2", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "2", "255", "255", "255", "2", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "2", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "2", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "2", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "2",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "2", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "2", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "2", "2", "2",
"2", "2", "2", "2", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255", "255", "255",
"255", "255", "255", "255", "255", "255", "255")
The first three lines are header and can be removed, but note that:
length(pgm) # 629
but 25*25+3
is 628
, so either the header consists of 4 elements, or there is another extra element at the end.
pgm = pgm[-(1:4)] # remove first 4 elements
pgm = as.numeric(pgm) # convert from character to numeric vector
pgm = matrix(pgm, 25, 25, byrow=TRUE) # populate matrix from pgm by rows
# pgm is now a matrix:
is.matrix(pgm) # TRUE
dim(pgm) # [1] 25 25