[R] can I do this with read.table??

Petr Pikal petr.pikal at precheza.cz
Mon Jan 30 14:32:44 CET 2006


Hi

Well, colClesses can be used for what you want to do. See

> test<-read.table("c:/temp/test.txt", 
colClasses=c("character","numeric", "character", "factor"))
> str(test)
`data.frame':   10 obs. of  3 variables:
 $ doba : num  189 256 286 105 272 45 29.5 43 68.5 99
 $ otac : chr  "0.6" "0.6" "0.6" "1.2" ...
 $ sklon: Factor w/ 6 levels "10","110","120",..: 2 6 5 5 1 2 3 6 5 4
>

As you see you can change numeric variable to character or factor as 
you wish.

Do not forget!!!
          Note that 'colClasses' is specified per column (not per
          variable) and so includes the column of row names (if any). 

HTH
Petr




On 26 Jan 2006 at 14:57, Douglas Grove wrote:

Date sent:      	Thu, 26 Jan 2006 14:57:17 -0800 (PST)
From:           	Douglas Grove <dgrove at fhcrc.org>
To:             	Kjetil Brinchmann Halvorsen <kjetilbrinchmannhalvorsen at gmail.com>
Copies to:      	r-help at stat.math.ethz.ch
Subject:        	Re: [R] can I do this with read.table??

> I did read the help page, very carefully.   
> 
> The colClasses argument can be used if I want
> to stop and look through every data set to see
> which column I need to protect.  But that's what I 
> said that I don't want to do.
> 
> As for 'as.is', I wish it did what you suggest, but
> it doesn't.  If one reads carefully, as.is protects
> a character vector from converstion to a *factor*,
> but not from conversion to numeric/logical.
> 
> Doug
> 
> 
> 
> 
> On Sun, 26 Feb 2006, Kjetil Brinchmann Halvorsen wrote:
> 
> > Douglas Grove wrote:
> > > Hi,
> > > 
> > > I'm trying to figure out if there's an automated way to get
> > > read.table to read in my data and *not* convert the character
> > > columns into anything, just leave them alone.  What I'm referring
> > 
> > ?Did you read the help page?
> > What about argument as.is=TRUE?
> > See also argument colClasses
> > 
> > Kjetil
> > 
> > > to as 'character columns' are columns in the data that are quoted.
> > > For columns of alphabetic strings (that aren't TRUE or FALSE) I
> > > can suppress conversion to factor with as.is=TRUE, but what I'd
> > > like to stop is the conversion of quoted numbers of the form
> > > "01","02",..., into numeric form.
> > > 
> > > By an 'automated way', I mean one that does not involve me having
> > > to know which columns in the data are the ones I want kept as they
> > > are.
> > > 
> > > This doesn't seem like an unreasonable thing to want to do.
> > > After all, say I've got the data.frame:
> > > 
> > > A <- data.frame(a=1:3, b=I(c("01","02","03")))
> > > 
> > > I can export this to a text file with the simple command
> > > 
> > > write.table(A, "A.txt", sep="\t", row.names=FALSE, quote=TRUE)
> > > 
> > > but I cannot find an equally simple mechanism for reading this
> > > data back in from A.txt that allows me to reconstruct my
> > > data.frame 'A'.  Is this an unreasonable thing to expect?
> > > 
> > > Thanks,
> > > Doug
> > > 
> > > ______________________________________________
> > > 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
> > > 
> > 
> >
> 
> ______________________________________________
> 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

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list