[R] read a file of text with read.table

Rolf Turner r.turner at auckland.ac.nz
Thu Jun 26 11:37:08 CEST 2014


On 26/06/14 21:21, carol white wrote:
> Hi,
>
> with read.fwf, it works.
>
>
> But I still don't understand why it doesn't work with read.table
> since the sep by default is "", which is the case and in one trial, I
> used  read.table("myfile",colClasses = "character",
> stringsAsFactors=FALSE, and stil didn't work but it should have.

What do you mean "didn't work"?  Do you mean you are still getting a 
factor?  Or just that you are not getting a 10-column data frame?

In respect of the latter:  RTFM.  It says, about "sep":

> If sep = "" (the default for read.table) the separator is ‘white space’,
> that is one or more spaces, tabs, newlines or carriage returns.

You *have to* use read.fwf() if you want to go this route.

I also gave you a (somewhat more convoluted but equally valid) procedure 
to achieve what you want.

What I would *really* like to know is:  How did you manage to get 
colClasses="character" *not* to work?  You must have done *something* 
strange.

A ***reproducible*** example would be nice.

cheers,

Rolf Turner



More information about the R-help mailing list