[R] convert numeric variables to factor

S Ellison S@E|||@on @end|ng |rom LGCGroup@com
Tue Apr 10 16:05:57 CEST 2018


> From: R-help [mailto:r-help-bounces using r-project.org] On Behalf Of Saif Tauheed
> 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?
> 

First, please post reproducible examples; your code will not run in anyone else's session (for example you have not shown where the data frame comes from), and if the code does not generate the error message reproducibly, noone can tell you exactly why it happens

Second, please _follow_ the posting guide and work through the suggested steps before posting a question to this list. An internet search for this error would have told you what causes it.

Finally: 
- Your loop will not work because your loop variable is I and the index is i; they are different in R. You'll just update the same column (i) lots of times, assuming i is defined.

- The error message arises from a call to factor() in as.factor(). It tells you that the column being converted is not a simple vector. That in turn tells me that you have not constructed your data frame correctly. I can't tell you what you did wrong there. 

- As another poster has said, data.frame is the name of a function - one that construicts a data frame. R can often tell which you want, but it is never safe to use the name of a function as the name of a data object. Use a different name for your data frame.

S Ellison


*******************************************************************
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If 
you have received this message in error, please notify the sender 
immediately via +44(0)20 8943 7000 or notify postmaster using lgcgroup.com 
and delete this message and any copies from your computer and network. 
LGC Limited. Registered in England 2991879. 
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK


More information about the R-help mailing list