Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: read.table and missing values

Tags:

r

na

read.table

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?

like image 857
CHONG Avatar asked Sep 04 '26 05:09

CHONG


1 Answers

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(...)
like image 94
Arno Avatar answered Sep 06 '26 20:09

Arno