Hi, I am also a quite new user of R and would like to ask you for help: I have a data frame where all columns are numeric variables. My aim is to convert one columnt in factors. Example: MD 0.2 0.1 0.8 0.3 0.7 0.6 0.01 0.2 0.5 1 1 I want to make classes: 0-0.2 A 0.21-0.4 B 0.41-0.6 C ..... and so on So after classification I wil get: MD A A D B . . . and so on Please could you give an advice to a newbie? Thanks a lot in advance.. Michael
Michael Assume your data frame is called "data" and your variable is called "V1". Converting this to a factor is: data$V1 <- factor(data$V1) Creating the classes can be done using ifelse(). Something like data$class <- ifelse(data$V1 < .21, A, ifelse(data$V1 < .41, B, C)) Harold> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ing. > Michal Kneifl, Ph.D. > Sent: Wednesday, July 18, 2007 1:37 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Classification > > Hi, > I am also a quite new user of R and would like to ask you for help: > I have a data frame where all columns are numeric variables. > My aim is to convert one columnt in factors. > Example: > MD > 0.2 > 0.1 > 0.8 > 0.3 > 0.7 > 0.6 > 0.01 > 0.2 > 0.5 > 1 > 1 > > > I want to make classes: > 0-0.2 A > 0.21-0.4 B > 0.41-0.6 C > ..... and so on > > So after classification I wil get: > MD > A > A > D > B > . > . > . > and so on > > Please could you give an advice to a newbie? > Thanks a lot in advance.. > > Michael > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Have a look at the recode function in the car package library(car) ?recode should give you what you need. --- "Ing. Michal Kneifl, Ph.D." <xkneifl at mendelu.cz> wrote:> Hi, > I am also a quite new user of R and would like to > ask you for help: > I have a data frame where all columns are numeric > variables. My aim is > to convert one columnt in factors. > Example: > MD > 0.2 > 0.1 > 0.8 > 0.3 > 0.7 > 0.6 > 0.01 > 0.2 > 0.5 > 1 > 1 > > > I want to make classes: > 0-0.2 A > 0.21-0.4 B > 0.41-0.6 C > ..... and so on > > So after classification I wil get: > MD > A > A > D > B > . > . > . > and so on > > Please could you give an advice to a newbie? > Thanks a lot in advance.. > > Michael > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
On Wed, 2007-07-18 at 19:36 +0200, Ing. Michal Kneifl, Ph.D. wrote:> Hi, > I am also a quite new user of R and would like to ask you for help: > I have a data frame where all columns are numeric variables. My aim is > to convert one columnt in factors. > Example: > MD > 0.2 > 0.1 > 0.8 > 0.3 > 0.7 > 0.6 > 0.01 > 0.2 > 0.5 > 1 > 1 > > > I want to make classes: > 0-0.2 A > 0.21-0.4 B > 0.41-0.6 C > ..... and so on > > So after classification I wil get: > MD > A > A > D > B > . > . > . > and so on > > Please could you give an advice to a newbie? > Thanks a lot in advance.. > > MichaelSee ?cut You can then do something like:> DFMD 1 0.20 2 0.10 3 0.80 4 0.30 5 0.70 6 0.60 7 0.01 8 0.20 9 0.50 10 1.00 11 1.00> cut(DF$MD, breaks = c(seq(0, 1, .2)), labels = LETTERS[1:5])[1] A A D B D C A A C E E Levels: A B C D E HTH, Marc Schwartz
maybe: x = c(.2, .1, .8, .3, .7, .6, .01, .2, .5, 1, 1) breaks = seq(0, 1, .2) LETTERS[1:(length(breaks)-1)][cut(x, breaks)] b On Jul 18, 2007, at 1:50 PM, Doran, Harold wrote:> Michael > > Assume your data frame is called "data" and your variable is called > "V1". Converting this to a factor is: > > data$V1 <- factor(data$V1) > > Creating the classes can be done using ifelse(). Something like > > data$class <- ifelse(data$V1 < .21, A, ifelse(data$V1 < .41, B, C)) > > Harold > > >> -----Original Message----- >> From: r-help-bounces at stat.math.ethz.ch >> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ing. >> Michal Kneifl, Ph.D. >> Sent: Wednesday, July 18, 2007 1:37 PM >> To: r-help at stat.math.ethz.ch >> Subject: [R] Classification >> >> Hi, >> I am also a quite new user of R and would like to ask you for help: >> I have a data frame where all columns are numeric variables. >> My aim is to convert one columnt in factors. >> Example: >> MD >> 0.2 >> 0.1 >> 0.8 >> 0.3 >> 0.7 >> 0.6 >> 0.01 >> 0.2 >> 0.5 >> 1 >> 1 >> >> >> I want to make classes: >> 0-0.2 A >> 0.21-0.4 B >> 0.41-0.6 C >> ..... and so on >> >> So after classification I wil get: >> MD >> A >> A >> D >> B >> . >> . >> . >> and so on >> >> Please could you give an advice to a newbie? >> Thanks a lot in advance.. >> >> Michael >> >> ______________________________________________ >> R-help at stat.math.ethz.ch 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. >> > > ______________________________________________ > R-help at stat.math.ethz.ch 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.
You can use 'cut':> xMD 1 0.20 2 0.10 3 0.80 4 0.30 5 0.70 6 0.60 7 0.01 8 0.20 9 0.50 10 1.00 11 1.00> cut(x$MD, breaks=seq(0,1,.2), include.lowest=TRUE, labels=LETTERS[1:5])[1] A A D B D C A A C E E Levels: A B C D E>On 7/18/07, Ing. Michal Kneifl, Ph.D. <xkneifl at mendelu.cz> wrote:> Hi, > I am also a quite new user of R and would like to ask you for help: > I have a data frame where all columns are numeric variables. My aim is > to convert one columnt in factors. > Example: > MD > 0.2 > 0.1 > 0.8 > 0.3 > 0.7 > 0.6 > 0.01 > 0.2 > 0.5 > 1 > 1 > > > I want to make classes: > 0-0.2 A > 0.21-0.4 B > 0.41-0.6 C > ..... and so on > > So after classification I wil get: > MD > A > A > D > B > . > . > . > and so on > > Please could you give an advice to a newbie? > Thanks a lot in advance.. > > Michael > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
For all who sent help on topic Classification: Thank you very much folks. I have got some inspiration how to solve this task. Michael ----- Original Message ----- From: "Marc Schwartz" <marc_schwartz at comcast.net> To: "Ing. Michal Kneifl, Ph.D." <xkneifl at mendelu.cz> Cc: <r-help at stat.math.ethz.ch> Sent: Wednesday, July 18, 2007 7:53 PM Subject: Re: [R] Classification> On Wed, 2007-07-18 at 19:36 +0200, Ing. Michal Kneifl, Ph.D. wrote: >> Hi, >> I am also a quite new user of R and would like to ask you for help: >> I have a data frame where all columns are numeric variables. My aim is >> to convert one columnt in factors. >> Example: >> MD >> 0.2 >> 0.1 >> 0.8 >> 0.3 >> 0.7 >> 0.6 >> 0.01 >> 0.2 >> 0.5 >> 1 >> 1 >> >> >> I want to make classes: >> 0-0.2 A >> 0.21-0.4 B >> 0.41-0.6 C >> ..... and so on >> >> So after classification I wil get: >> MD >> A >> A >> D >> B >> . >> . >> . >> and so on >> >> Please could you give an advice to a newbie? >> Thanks a lot in advance.. >> >> Michael > > See ?cut > > You can then do something like: > >> DF > MD > 1 0.20 > 2 0.10 > 3 0.80 > 4 0.30 > 5 0.70 > 6 0.60 > 7 0.01 > 8 0.20 > 9 0.50 > 10 1.00 > 11 1.00 > > >> cut(DF$MD, breaks = c(seq(0, 1, .2)), labels = LETTERS[1:5]) > [1] A A D B D C A A C E E > Levels: A B C D E > > > HTH, > > Marc Schwartz > >