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
Just cast it! ?factor Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Apr 9, 2018 at 9:28 AM, Saif Tauheed <saif.tauheed at gmail.com> wrote:> 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 at 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.
see also ?cut if this is what you mean. -- Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Apr 9, 2018 at 9:28 AM, Saif Tauheed <saif.tauheed at gmail.com> wrote:> 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 at 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.
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 at r-project.org> On Behalf Of Saif Tauheed Sent: Monday, April 9, 2018 11:29 AM To: r-help at 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 at 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.
Or use cut():> Num <- c(2.2, 2.4, 3.5, 5, 7) > cut(Num, breaks=c(0,2,4,6), labels=c("Low","Medium","High"))[1] Medium Medium Medium High <NA> Levels: Low Medium High Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Apr 9, 2018 at 10:00 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote:> Just cast it! > > ?factor > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Mon, Apr 9, 2018 at 9:28 AM, Saif Tauheed <saif.tauheed at gmail.com> > wrote: > > 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 at 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. > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]
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 at r-project.org> On Behalf Of Saif Tauheed > Sent: Monday, April 9, 2018 11:29 AM > To: r-help at 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 at 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. > > ______________________________________________ > R-help at 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. >