[R] read.table and missing values

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jun 19 16:23:53 CEST 2001


On Tue, 19 Jun 2001, Peter Breuer wrote:

> I'd like to share data files (tab delimited text files)
> that i read in via read.table with other applications.
> Missing data are empty fields (two tabs following each other)
> I couldn't find a way yet to 'convince' R to interprete this to be
> missing values.

It's the default ....

> The read.table option na.strings="" or na.strings='' didn't work.
> using any special character in the data file for missings (like '#')
> and defining it for missing using na.strings="#" worked.
> So I was able to solve the problem doing a global replace.
> But I'd prefer to read in the data files as is and wonder whether
> there is a possibility to that in R.
> Data Import/Export Manual says that empty fields in numeric columns
> are regarded as missing values.
> But with my versions of R (Windows 1.2.3, Mac) it didn't work
> Documention, FAQ and the mailing archive didn't have any further hint.
> Thanks for any advice

It works as documented, and as documented does what you want (so no
further hint is needed).  For file foo.dat

a,b,c
1,2,3
4,,6
,8,9

I get

> read.table("foo.dat", header=T, sep=",")
   a  b c
1  1  2 3
2  4 NA 6
3 NA  8 9

as advertised.

You have set sep="\t", haven't you?  The default separator is white space,
and that will swallow two tabs.  The following had tabs in originally,

a	b	c
1	2	3
4		6
	8	9

and gives

> read.table("foo.dat", header=T, sep="\t")
   a  b c
1  1  2 3
2  4 NA 6
3 NA  8 9

So, if you do that it should work for you too.  And if you want to claim
on R-help that things do not work, please include an example so we can see
what you are doing (or not doing).

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list