[R] Errors in reading in txt files

Berend Hasselman bhh at xs4all.nl
Thu Dec 14 21:16:13 CET 2017


> On 14 Dec 2017, at 20:01, Ista Zahn <istazahn at gmail.com> wrote:
> 
> On Thu, Dec 14, 2017 at 1:58 PM, Berend Hasselman <bhh at xs4all.nl> wrote:
>> 
>>> On 14 Dec 2017, at 19:36, lily li <chocold12 at gmail.com> wrote:
>>> 
>>> Hi R users,
>>> 
>>> I have a question about reading from text files. The file has the structure
>>> below:
>>> 
>>> Time                            Column1   Column2
>>> 01.01.2001-12:00:00
>> 
>> This line does not contain 3 elements; only one.
>> You'll have to fix that line. Delete it, prepend it with a comment character of add enough columns.
> 
> I definitely don't recommend that. Instead, read
> 
> ?read.table
> 
> to learn about the "fill" and "header" arguments.
> 
> df = read.table("DATAM", header = TRUE, fill = TRUE)
> 
> will probably work.
> 

Yes. I agree. It's much better.
I should have experimented some more.

Berend

> Best,
> Ista
> 
> 
>> 
>> 
>> Berend
>> 
>>> 01.01.2001-24:00:00        12             11
>>> 01.02.2001-12:00:00        13             10
>>> 01.02.2001-24:00:00        11             12
>>> 01.03.2001-12:00:00        15             11
>>> 01.03.2001-24:00:00        16             10
>>> ...
>>> 
>>> I just use the simple script to open it: df = read.table('DATAM', head=T).
>>> 
>>> But it has the error and thus cannot read the file:
>>> Error in scan(file = file, what = what, sep = sep, quote = quote, dec =
>>> dec,  :
>>> line 1 did not have 3 elements
>>> 
>>> How to read it with three fixed columns, and how to read the time format in
>>> the first column correctly? Thanks for your help.
>>> 
>>>      [[alternative HTML version deleted]]
>>> 
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.



More information about the R-help mailing list