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!
To convert
vectortomatrix, you can simply use thematrixfunction:Depending if the data should be populated by rows instead of by columns, you can specify the parameter
byrow=TRUEIn your particular case, read the file with
readLines:For other users that do not have access to your file, here is output from
dputthat can be simply copypasted into R console:The first three lines are header and can be removed, but note that:
but
25*25+3is628, so either the header consists of 4 elements, or there is another extra element at the end.