[R] Create factors in R for more than one variable

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Oct 28 14:21:16 CEST 2004

On Thu, 28 Oct 2004, H J Gerber wrote:

> Hi all,
> I'm using R2.0 on linux. 

As there is no such version, unlikely.

> I have fixedwidth data consisting of 50 questions on a likert scale. The raw data is coded 1 to 5. I used read.fwf() to read in the data. 
> The problem is that factors isn't created automatically, because the data is coded numerically.
> To make factors out of the 50 questions I must do:
> V1 <- factor(V1)
> I don't want to do it 50 times. Is there a quicker way without using loops?

Not really (and a loop will a matter of seconds at most), but there are 
neater ways such as

DF[] <- lapply(DF as.factor)

which will convert it for you.  Or you could get read.fwf() to do so: see
the colClasses argument to read.table, as mentioned on its help page.

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