R: read.table and missing values

14,400

You can try to use the read.table function with fill= TRUE.

read.table(file =file, sep = sep, fill=TRUE)

If this does not work, I would suggest to try the readLines function instead of read.table.

readLines(...)
Share:
14,400
CHONG
Author by

CHONG

Updated on June 26, 2022

Comments

  • CHONG
    CHONG almost 2 years

    When I load my data file in tab delimited format in R, I got this error message:

    Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  : line 3 did not have 5 elements
    

    Here's my data:

    KEY ID      code1   code2   name
    1   sadsa   32423   344     ffsadsa
    2   vdffsfs 21344   234     fsadfgg
    3   3e4dsa  21321   #N/A    #N/A
    4   dcxzc   23421   #N/A    #N/A
    5   xzzcc   21223   124     erfsacf
    6   sdas    21321   464     fsadfsa
    7   assdad  32132   455     fsadfda
    

    I can see that the error is caused by the "#N/A" value in my data. I have tried the read.table option such as na.strings or comment.char = "#" but it still did not work.

    Is there any ways to keep the actual text (#N/A) or at least replace it with N/A when loading the data in R?