Hi all, I'm using R2.0 on linux. 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? Thanks Hennie Hennie Gerber Statistician UNISA - Research Support + 27 12 429 3188 Waarskuwing! Die sienings uitgespreek is my eie en nie noodwendig my werkgewer sin nie Warning! All views expressed are my own and not necessarily that of my employer. --------------------------------------------------------------------------- This message (and attachments) is subject to restrictions and a disclaimer. Please refer to http://www.unisa.ac.za/disclaimer for full details. --------------------------------------------------------------------------- <<<<gwavasig>>>> <<<< gwavasig >>>> [[alternative HTML version deleted]]
Prof Brian Ripley
2004-Oct-28 12:21 UTC
[R] Create factors in R for more than one variable
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
Thanks Prof Ripley, The statement: DF[] <- lapply(DF, as.factor) works perfectly and saves a lot of time! Regards Hennie>As there is no such version, unlikely.> 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.--------------------------------------------------------------------------- This message (and attachments) is subject to restrictions and a disclaimer. Please refer to http://www.unisa.ac.za/disclaimer for full details. --------------------------------------------------------------------------- <<<<gwavasig>>>> <<<< gwavasig >>>> [[alternative HTML version deleted]]