Hi, I was hoping someone could help me. I am a graduate student new to using R, and I'm trying to figure out how to recode a continuous variable to make it into an ordinal variable with 3 categories. I literally have no idea how to proceed--could anyone possibly advise me? Please copy me on any responses, as I have just subscribed to the R-help email list but don't know whether the subscription has gone through yet. Thanks so much- Shayna
Shayna Strom wrote:> Hi, > > I was hoping someone could help me. I am a graduate student new to using R, and I'm trying to figure out how to recode a continuous variable to make it into an ordinal variable with 3 categories. I literally have no idea how to proceed--could anyone possibly advise me? Please copy me on any responses, as I have just subscribed to the R-help email list but don't know whether the subscription has gone through yet. > > Thanks so much- > Shayna > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlSee ?ordered. x <- rep(runif(3), 10) xord <- ordered(x)
Shayna Strom wrote:> Hi, > > I was hoping someone could help me. I am a graduate student new to using R, and I'm trying to figure out how to recode a continuous variable to make it into an ordinal variable with 3 categories. I literally have no idea how to proceed--could anyone possibly advise me? Please copy me on any responses, as I have just subscribed to the R-help email list but don't know whether the subscription has gone through yet. > > Thanks so much- > Shayna > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.htmlSorry sent too quickly. Probably what you want is ?cut too. x <- runif(30) xcut <- cut(x, c(0, 1/3, 2/3, 1)) xord <- ordered(xcut)
Basically, you want to use cut(). For example, x <- 1:20 cut(x, c(0, 8, 15, 20)) This divides x into a factor with levels (0,8], (8,15], and (15,20]. -roger Shayna Strom wrote:> Hi, > > I was hoping someone could help me. I am a graduate student new to using R, and I'm trying to figure out how to recode a continuous variable to make it into an ordinal variable with 3 categories. I literally have no idea how to proceed--could anyone possibly advise me? Please copy me on any responses, as I have just subscribed to the R-help email list but don't know whether the subscription has gone through yet. > > Thanks so much- > Shayna > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
(tst <- cut(1:11, c(-Inf, 3, 5, Inf))) [1] (-Inf,3] (-Inf,3] (-Inf,3] (3,5] (3,5] (5,Inf] (5,Inf] (5,Inf] [9] (5,Inf] (5,Inf] (5,Inf] Levels: (-Inf,3] (3,5] (5,Inf] > > class(tst) [1] "factor" hope this helps. spencer graves Shayna Strom wrote:>Hi, > >I was hoping someone could help me. I am a graduate student new to using R, and I'm trying to figure out how to recode a continuous variable to make it into an ordinal variable with 3 categories. I literally have no idea how to proceed--could anyone possibly advise me? Please copy me on any responses, as I have just subscribed to the R-help email list but don't know whether the subscription has gone through yet. > >Thanks so much- >Shayna > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
Hi!> using R, and I'm trying to figure out how to recode a continuous > variable to make it into an ordinal variable with 3 categories.You are looking for cut():> a <- runif(15)> a[1] 0.19109987 0.78808597 0.78458256 0.31355035 0.02076274 0.82287287 0.75260382 0.82627690 0.14775167 [10] 0.59427620 0.12314764 0.44151537 0.05123785 0.88879744 0.25552054> b = cut(a, breaks=c(0, 0.33, 0.66, 1), labels=c('low', 'medium', 'high'))> b[1] low high high low low high high high low medium low medium low high low Levels: low medium high cu Philipp -- Dr. Philipp Pagel Tel. +49-89-3187-3675 Institute for Bioinformatics / MIPS Fax. +49-89-3187-3585 GSF - National Research Center for Environment and Health Ingolstaedter Landstrasse 1 85764 Neuherberg, Germany http://mips.gsf.de/~pagel