0

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!

Phil
  • 7,287
  • 3
  • 36
  • 66
Cbrady
  • 93
  • 1
  • 2
  • 7
  • What is a `pgm` file? How does it look like? Are we supposed to magically know its content? Please provide Minimal Reproducible Example, including sample data https://stackoverflow.com/a/5963610/ – Colombo Mar 04 '21 at 21:45
  • https://www.dropbox.com/s/wpyvsbyc5tooqgm/1_1.pgm?dl=0 – Cbrady Mar 04 '21 at 21:53
  • a pgm file can be viewed in gimp but also holds the numeric values of the colours in an image, e.g. 128>= is white and <128 is black. I uploaded the file if you want to have a look and as you can see its just more or less 1 column but i need to convert it into a 25x25 csv file – Cbrady Mar 04 '21 at 21:55

1 Answers1

1

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
Colombo
  • 428
  • 3
  • 12
  • That works when I copy and paste the values from the file in just as you did, is there no way I can use read.delim to read in the values then do the same thing without having to copy from the file itself? I tried this df <- read.delim("1_1.pgm", skip =3, row.names = NULL, header = FALSE) but then everything just goes wrong in the csv file, theres so many more rows/columns – Cbrady Mar 04 '21 at 22:55
  • Thank you for your response tho, its been very helpful! – Cbrady Mar 04 '21 at 22:55
  • Check out the first line in the code. I edited it for a better clarity. No need to use `read.delim`, you are really not reading a delimited file (at least in your example, I don't know if it generally holds for all pgms). Just read the whole file with `readLines("1_1.pgm")`. – Colombo Mar 04 '21 at 23:00
  • Ahhh i see, damn i was stuck trying to figure out how the read.delim was messing it up so much and I think i could have solved it if just knew about the readLines. Thank you very much! :) – Cbrady Mar 04 '21 at 23:04