Search code examples
rread.fwf

R - How to Read in a .fwf that contains a # sign


I am attempting to read in a large fixed width file into R using read.fwf, but I keep getting the error "Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : line 47 did not have 41 elements". My dataset has 41 columns but when I read in line 48 alone I only get 27 columns. I noticed that the error takes place when it encounters a # sign. How can I either remove the # sign or force read.fwf to ignore it. Here is a little of my code, but since the dataset is massive I am not going to provide it.

df <- read.fwf("flat404.DALLASCOUNTY.20161030.412476", widths = width2, col.names = fields)

Thanks for any help.


Solution

  • We can set the comment.char argument to blanks or a character that is not a #

    df <- read.fwf("flat404.DALLASCOUNTY.20161030.412476", 
             widths = width2, col.names = fields, comment.char = "")