Hi, Is there a way to easily convert the list of course terms into sequential integers in the dataframe (see code below)? eg. "199801" = 1; "199808"=2 I know I can use recode but shouldn't "which" work? Thanks in advance! sc = data.frame(c("200208", "200701", "201201")) names(sc) = c("TERM") TermList = c(NA, "199801", "199808", "199901", "199908", "200001", "200008", "200101", "200108", "200201", "200208", "200301", "200308", "200401", "200408", "200501", "200508", "200601", "200608", "200701", "200708", "200801", "200808", "200901", "200908", "201001", "201008", "201101", "201108", "201201", "201308", "201401", "201408") which(TermList==sc$TERM) [[alternative HTML version deleted]]
?factor You might find the description in the Introduction to R that comes with the R software helpful if the help page seems terse. Please read the Posting Guide and particular post using plain text format, a strong in your email program. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On January 22, 2015 8:01:25 AM PST, Charles Stangor <cstangor at gmail.com> wrote:>Hi, > >Is there a way to easily convert the list of course terms into >sequential >integers in the dataframe (see code below)? > >eg. "199801" = 1; "199808"=2 > >I know I can use recode but shouldn't "which" work? > >Thanks in advance! > > >sc = data.frame(c("200208", "200701", "201201")) >names(sc) = c("TERM") > >TermList = c(NA, "199801", "199808", "199901", "199908", "200001", >"200008", "200101", "200108", > "200201", "200208", "200301", "200308", "200401", "200408", >"200501", "200508", > "200601", "200608", "200701", "200708", "200801", "200808", >"200901", "200908", > "201001", "201008", "201101", "201108", "201201", "201308", >"201401", "201408") > >which(TermList==sc$TERM) > > [[alternative HTML version deleted]] > >______________________________________________ >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.
I'm not quite sure, but I think you might want:> which(TermList %in% sc$TERM)[1] 11 20 30 instead. Using == ends up with automatic recycling and other things you probably weren't expecting. Sarah On Thu, Jan 22, 2015 at 11:01 AM, Charles Stangor <cstangor at gmail.com> wrote:> Hi, > > Is there a way to easily convert the list of course terms into sequential > integers in the dataframe (see code below)? > > eg. "199801" = 1; "199808"=2 > > I know I can use recode but shouldn't "which" work? > > Thanks in advance! > > > sc = data.frame(c("200208", "200701", "201201")) > names(sc) = c("TERM") > > TermList = c(NA, "199801", "199808", "199901", "199908", "200001", > "200008", "200101", "200108", > "200201", "200208", "200301", "200308", "200401", "200408", > "200501", "200508", > "200601", "200608", "200701", "200708", "200801", "200808", > "200901", "200908", > "201001", "201008", "201101", "201108", "201201", "201308", > "201401", "201408") > > which(TermList==sc$TERM) >-- Sarah Goslee http://www.functionaldiversity.org [[alternative HTML version deleted]]
You did not show what answer you expected, but does the following do what you want? > match(sc$TERM, TermList) [1] 11 20 30 Making a factor whose levels are TermList may also be useful. (The exclude=NULL is to factor doesn't drop NA from the levels). > sc$fTERM <- factor(sc$fTERM, levels=TermList, exclude=NULL) > str(sc) 'data.frame': 3 obs. of 2 variables: $ TERM : Factor w/ 3 levels "200208","200701",..: 1 2 3 $ fTERM: Factor w/ 33 levels NA,"199801","199808",..: 11 20 30 Bill Dunlap TIBCO Software wdunlap tibco.com On Thu, Jan 22, 2015 at 8:01 AM, Charles Stangor <cstangor at gmail.com> wrote:> Hi, > > Is there a way to easily convert the list of course terms into sequential > integers in the dataframe (see code below)? > > eg. "199801" = 1; "199808"=2 > > I know I can use recode but shouldn't "which" work? > > Thanks in advance! > > > sc = data.frame(c("200208", "200701", "201201")) > names(sc) = c("TERM") > > TermList = c(NA, "199801", "199808", "199901", "199908", "200001", > "200008", "200101", "200108", > "200201", "200208", "200301", "200308", "200401", "200408", > "200501", "200508", > "200601", "200608", "200701", "200708", "200801", "200808", > "200901", "200908", > "201001", "201008", "201101", "201108", "201201", "201308", > "201401", "201408") > > which(TermList==sc$TERM) > > [[alternative HTML version deleted]] > > ______________________________________________ > 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]]
Dear Charles, When you put TERM in a data frame, by default it was converted to a factor, which is already stored as integers. Thus, if I understand correctly what you want, the following will work:> sc$TERM[1] 200208 200701 201201 Levels: 200208 200701 201201> unclass(sc$TERM)[1] 1 2 3 attr(,"levels") [1] "200208" "200701" "201201"> class(unclass(sc$TERM))[1] "integer" Note that the original class designations are presented in the "levels" attribute. This assumes that all of the class designations that you want to use appear in sc$TERM. You already received some suggestions for the way that you originally posed the problem (where there were classes in TermList not appearing in sc$TERM). I hope this helps, John ------------------------------------------------------- John Fox, Professor McMaster University Hamilton, Ontario, Canada http://socserv.mcmaster.ca/jfox/> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Charles > Stangor > Sent: January-22-15 11:01 AM > To: r-help at r-project.org > Subject: [R] 'which' statement for recode? > > Hi, > > Is there a way to easily convert the list of course terms into sequential > integers in the dataframe (see code below)? > > eg. "199801" = 1; "199808"=2 > > I know I can use recode but shouldn't "which" work? > > Thanks in advance! > > > sc = data.frame(c("200208", "200701", "201201")) > names(sc) = c("TERM") > > TermList = c(NA, "199801", "199808", "199901", "199908", "200001","200008",> "200101", "200108", > "200201", "200208", "200301", "200308", "200401", "200408","200501",> "200508", > "200601", "200608", "200701", "200708", "200801", "200808","200901",> "200908", > "201001", "201008", "201101", "201108", "201201", "201308","201401",> "201408") > > which(TermList==sc$TERM) > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.--- This email has been checked for viruses by Avast antivirus software. http://www.avast.com