[R] convert 400 numeric variables to categorical together

John Kane jrkrideau at inbox.com
Tue May 8 19:45:58 CEST 2012


Here is an example that may help. I found the idea somewhere in the R-help archives but don't have a reference any more.

mydata  <- data.frame(a1 = 1:5, a2 =  2:6, a3 = 3:7)
str(mydata)
mydata[, 1:2] <- lapply(mydata[,1:2], factor) 
str(mydata)


so basically all you need to do is specific what columns you want to convert to factors and use lapply.

Good luck and BTW Rcmdr is a nice GUI but it is much more effiient in the long run to use a command line interface, combinded with a good text editior (Tinn-R is nice for Windows)

John Kane
Kingston ON Canada


> -----Original Message-----
> From: xinxi813 at 163.com
> Sent: Tue, 8 May 2012 20:01:28 +0300
> To: r-help at r-project.org
> Subject: [R] convert 400 numeric variables to categorical together
> 
> 
> 
> 
> 
> 
> Hi Jim and Michael,
> 
> Thank you very much for replying.
> 
> Here is the information about my data. I have a data frame, including
> more than 800 variables(columns) and 30000 cases(rows).400 of those
> variables are categorical variables. I used to use Rcmdr to convert
> variables, however, when the number of variables I need to convert
> getting more, I need to keep left clicking mouse to confirm the
> conversion (400 times) because as.factor() can only convert 1 variable at
> a time. So I am thinking if there is any faster way. Thank you very much.
> 
> Best regards,
> 
> ya
> 
> 
> 
> ya
> 
> From: R. Michael Weylandt
> Date: 2012-05-08 19:35
> To: xinxi813
> CC: r-help
> Subject: Re: [R] convert 400 numeric variables to categorical together
> How are they arranged currently? And should they be all one set of
> levels or different factor sets?
> 
> Michael
> 
> On Tue, May 8, 2012 at 12:32 PM, ya <xinxi813 at 163.com> wrote:
>> Hi everyone,
>> 
>> Is there anyway I can convert more than 400 numeric variables to
>> categorical variables simultaneously?
>> 
>> as.factor() is really slow, and only one at a time.
>> 
>> Thank you very much.
>> 
>> ya
>>        [[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails



More information about the R-help mailing list