[R] convert numeric variables to factor

Michael Dewey ||@t@ @end|ng |rom dewey@myzen@co@uk
Tue Apr 10 15:05:48 CEST 2018


Not sure whether this is the problem but calling your data frame 
data.frame is not a good idea.

On 10/04/2018 11:48, Saif Tauheed wrote:
> Thank you very much.
> 
> After that I have the following error:
> 
> cols<- c("GrMM", "RELG", "CASTE1", "SECTOR", "SECTOR4","AGE", "MARITAL", "JOBSTATUS", "ENG", "EDU", "PARENT_EDU", "MASSMEDIA_F", "MASSMEDIA_M", "HomeComputer", "HomeInternet")
>> for (I in cols) {data.frame[,i]= as.factor(data.frame[,i])}
> 
> 
> Error in sort.list(y) : 'x' must be atomic for 'sort.list'
> Have you called ‘sort’ on a list?
> 
> 
> 
> 
> 
> 
>> On 10-Apr-2018, at 3:12 PM, Eric Berger <ericjberger using gmail.com> wrote:
>>
>> You are missing a comma between "MARITAL" and "JOBSTATUS".
>>
>> On Tue, Apr 10, 2018 at 10:27 AM, Saif Tauheed <saif.tauheed using gmail.com <mailto:saif.tauheed using gmail.com>> wrote:
>> I run this command for converting the numerical variable into factor. However, I get the following error message.
>>
>>> cols<- c(“GrMM", "RELG", "CASTE1", "SECTOR", "SECTOR4","AGE", "MARITAL" "JOBSTATUS", "ENG", "EDU", "PARENT_EDU", "MASSMEDIA_F", "MASSMEDIA_M", "HomeComputer", "HomeInternet") for (i in cols) {data.frame[,i]= as.factor(data.frame[,i])}
>>
>>
>> Error: unexpected string constant in “cols<- c(“GrMM", "RELG", "CASTE1", "SECTOR", "SECTOR4","AGE", "MARITAL" "JOBSTATUS""
>>
>> Please help.
>>
>> Regards
>> Afzal
>>
>>
>>> On 10-Apr-2018, at 12:14 AM, Rui Barradas <ruipbarradas using sapo.pt <mailto:ruipbarradas using sapo.pt>> wrote:
>>>
>>> Hello,
>>>
>>> Though Bert's and David's answers are what you should do, note that some R functions that need factors will coerce their input variables when necessary.
>>> Have you tried to run the code you haven't posted without coercing to factor? It might run...
>>>
>>> Hope this helps,
>>>
>>> Rui Barradas
>>>
>>> On 4/9/2018 6:11 PM, David L Carlson wrote:
>>>> Try the help files:
>>>> ?factor
>>>> ----------------------------------------
>>>> David L Carlson
>>>> Department of Anthropology
>>>> Texas A&M University
>>>> College Station, TX 77843-4352
>>>> -----Original Message-----
>>>> From: R-help <r-help-bounces using r-project.org <mailto:r-help-bounces using r-project.org>> On Behalf Of Saif Tauheed
>>>> Sent: Monday, April 9, 2018 11:29 AM
>>>> To: r-help using r-project.org <mailto:r-help using r-project.org>
>>>> Subject: Re: [R] convert numeric variables to factor
>>>> Dear Sir,
>>>> I have xlsx data set which I have imported to R studio. Now some of the variables are defined as numeric but I want define them as factor variable so that I run classification algorithm in R.
>>>> Please to covert the variables.
>>>> Thanks and Regards
>>>> Abu Afzal
>>>> PhD Eco
>>>> JNU
>>>> India
>>>> ______________________________________________
>>>> R-help using r-project.org <mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help <https://stat.ethz.ch/mailman/listinfo/r-help>
>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <http://www.r-project.org/posting-guide.html>
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>> ______________________________________________
>>>> R-help using r-project.org <mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat.ethz.ch/mailman/listinfo/r-help <https://stat.ethz.ch/mailman/listinfo/r-help>
>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <http://www.r-project.org/posting-guide.html>
>>>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>          [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org <mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help <https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html <http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
> 

-- 
Michael
http://www.dewey.myzen.co.uk/home.html




More information about the R-help mailing list