jpm miao
2015-Sep-14 17:35 UTC
[R] Is there any concise way to write a one-to-one mapping?
My code is: if(type=="none") type2<-"nc" if(type=="drift") type2<-"c" if(type=="trend") type2<-"ct" I am wondering if there's a concise way to write a mapping from type to type2, especially when the number of categories is high. Thanks! [[alternative HTML version deleted]]
Jeff Newmiller
2015-Sep-14 17:41 UTC
[R] Is there any concise way to write a one-to-one mapping?
Assuming your data are in a data frame, make anorher data frame with the type
and type2 columns and merge it with your original data frame.
?merge
---------------------------------------------------------------------------
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 September 14, 2015 10:35:38 AM PDT, jpm miao <miaojpm at gmail.com>
wrote:>My code is:
>
>
>
>if(type=="none")
>
> type2<-"nc"
>
>if(type=="drift")
>
> type2<-"c"
>
>if(type=="trend")
>
> type2<-"ct"
>
>
>I am wondering if there's a concise way to write a mapping from type to
>type2, especially when the number of categories is high. Thanks!
>
> [[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.
Duncan Murdoch
2015-Sep-14 18:15 UTC
[R] Is there any concise way to write a one-to-one mapping?
On 14/09/2015 1:35 PM, jpm miao wrote:> My code is: > > > > if(type=="none") > > type2<-"nc" > > if(type=="drift") > > type2<-"c" > > if(type=="trend") > > type2<-"ct"These are concise: type2 <- c(none = "nc", drift = "c", trend = "ct")[type] type2 <- switch(type, none = "nc", drift = "c", trend = "ct") Duncan Murdoch> > > I am wondering if there's a concise way to write a mapping from type to > type2, especially when the number of categories is high. Thanks! > > [[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.