[R] read.table error

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Sep 4 14:09:35 CEST 2008


On Thu, 4 Sep 2008, Steve Murray wrote:

>
> Dear all,
>
> I have a tab-delimited text (.txt) file which I'm trying to read into R. 
> This file is of column format - there are in fact 3 columns and 259201 
> rows (including the column headers). I've been using the following 
> commands, but receive an error each time which prevents the data from 
> being read in:

How about telling R it is tab-delimited?  Use read.delim or at least 
sep="\t".

Also, see the footer to this message: we are not clairvoyant and cannot 
see the file unless you show us part of it.

With a file of that size you should study the 'R Data Import/Export 
Manual' and take some steps to read it in efficiently (e.g. specify 
nrows and colClasses).

>> Jan <- read.table("JanuaryAvBurntArea.txt", header=TRUE)
> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
>  line 1 did not have 6 elements
>
>
> I tried removing the 'header' argument, but receive a similar message:
>
>> Jan <- read.table("JanuaryAvBurntArea.txt")
> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
>  line 2 did not have 6 elements
>
>
> What's more confusing about this is that I know that none of the lines 
> have 6 elements! They're not supposed to! Each row only has 3 values 
> (one per column)!

I suspect there is whitespace in the 'values'.

>
> As a final resort I tried 'scan':
>
> <- scan("JanuaryAvBurntArea.txt")
> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
>  scan() expected 'a real', got 'Latitude'
>
> ...which is obviously something to do with there being a header as the first row, but the 'scan' command doesn't seem to have an equivalent of 'header=TRUE' like read.table...?
>
>
> If anyone is able to shed some light on why I'm receiving these errors, and how I can get the data into R, then I'd be very grateful to hear them! I suspect I'm doing something very basic which is wrong!
>
> Many thanks,
>
> Steve
>
>
>
>
> _________________________________________________________________
> Win New York holidays with Kellogg’s & Live Search
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list