0

I am currently tasked with manipulating a text file in order to eliminate any whitespace from the end of a file. To do this, I am toying with R and trying to find a way to cut the spaces at the end.

Each line of the text file has a different number of variables on, separated by commas like below in the simplified example:

This is the format of the text file before imported into R:

This is the format of the text file before imported into R

As you can see, the length of each line is different. R struggles to take this into account, cutting down each line to the same length as the top line. Does anyone know why this may be?

This is the format of the data frame in R:

This is the format of the data frame in R

My code to read the text file is as follows:

write.table(sample_file, "txt_file.txt", sep = ",", quote = FALSE)

Any suggestions would be greatly appreciated. Let me know if something doesn't make sense.

benson23
  • 16,369
  • 9
  • 19
  • 38
  • I would read the file line by line to not have this issue (but maybe there is a function to deal with such a thing in R) – glagla Oct 30 '20 at 17:07
  • Maybe this helps to answer your question https://stackoverflow.com/questions/18922493/how-can-you-read-a-csv-file-in-r-with-different-number-of-columns – stefan Oct 30 '20 at 17:10

0 Answers0