[R] rewrite a data file use write.table(), count.fields() show different pattern, any suggestion appreciated.

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue May 22 14:12:47 CEST 2007


If you write out unquoted fields, how do you know they do not contain 
tabs?

The default is quote=TRUE for a good reason.

On Tue, 22 May 2007, Yong Wang wrote:

> Dear all:
>
> I read in a tab delimited dataset, and then write it out as another
> file as following: I did this simply to make sure I understand the
> behavior of this command.
>
> data<-read.table(file,header=F,sep="\t",fill=T,colClasses="character");
> write.table(data,file="newdata.txt",eol="\n",sep="\t",quote=F,row.names=F);
>
>
> cf1 <- count.fields(newdata.txt, sep="\t")
> table(cf1)
> 13   17       23
> 10   126   5445
>
> # is different to
>
> cf2 <- count.fields(file,sep="\t")
> 13   17       23        33
> 10   106   5433      32
>
> the worst problem is the maximal value of cf1 (33) is larger than the
> maximal value of cf2 (23) which is the right number of fields for most
> rows in the original file.
>
> I need to use write.table for some important data manipulation work,
> your suggestion is
> highly appreciated.
>
> Best Regards
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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