[R] Re: reading in columns of a data set as factors

Bill Venables venables at acland.qld.cmis.csiro.au
Thu Apr 27 08:02:17 CEST 2000


> Dear people,
> 
> Replying to my own message here. The following appears to work. I read in
> the data using read.table and then coerce the columns of the data frame to
> factors afterwards.
> 
> clus.df <- read.table("clus.dat",header= FALSE, row.names=NULL,
> col.names=names)
> 

Ah.  So you don't have the column names as part of the file but
in a separate vector, 'names'.  The way I would do it would be
(warning: untested code...)

template <- structure(c(as.list(rep("", 66)), as.list(rep(0, 
33)),
			names = names)
clus.df <- data.frame(scan("clus.dat", what = template))

I'd be surprised if it made much difference in time or resources,
though.  Sometimes a loop is OK.



> attach(clus.df)
> 
> for(i in 1:66)
> clus.df[,i] <- as.factor(clus.df[,i])
> 
> However, I don't find this completely satisfactory. For one thing, I have
> somehow got the impression that loops in Splus/R should be avoided
> whenever possible (gee, I wonder where I got that impression) and so is
> there a more elegant way to do this (without loops)?
> 
>                                                    Faheem.
> 
> On Wed, 26 Apr 2000, Faheem Mitha wrote:
> 
> > Dear people,
> > 
> > I've spent some time trying to find a simple way to do the following. I
> > can certainly think of complicated ways to do it...
> > 
> > I have a data set of 99 columns and 2000 rows. Each row corresponds to an
> > individual item of data, each column corresponds to a variable. I want
> > this data to be read into a data frame. The first 66 columns are binary,
> > values 0, 1, and I want these to be coerced into factor form. The last
> > 33 are ordinary numeric data. For
> > concreteness let us call these variables b1... b66 and the last 33
> > x1... x33.
> [stuff snipped]
> 

-- 
Bill Venables,      Statistician,     CMIS Environmetrics Project
CSIRO Marine Labs, PO Box 120, Cleveland, Qld,  AUSTRALIA.   4163
Tel: +61 7 3826 7251           Email: Bill.Venables at cmis.csiro.au

Fax: +61 7 3826 7304      http://www.cmis.csiro.au/bill.venables/



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list