[R] Is there any concise way to write a one-to-one mapping?

Duncan Murdoch murdoch.duncan at gmail.com
Mon Sep 14 20:15:59 CEST 2015


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.



More information about the R-help mailing list