Search code examples
rpgm

Convert pgm to Matrix


I need to convert a pgm file to a csv file with 25 rows by 25 columns.

df <- read.delim("1_1.pgm", skip =3, row.names = NULL, sep = "," ,col.names = NULL, header = FALSE, fill = TRUE)
write.csv(df, "test.csv")

I've done this but I think I have to convert it to a matrix as the above code is just a single column followed by the list of numbers so instead of having it by 25x25 it is like 625 numbers in one column.

Can anyone provide any help on how I take the text file and change it to a matrix with the 25x25?

Thanks!


Solution

  • 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