[R] Reclassify string values

Peter Langfelder peter.langfelder at gmail.com
Thu Nov 3 20:13:32 CET 2011


On Thu, Nov 3, 2011 at 11:59 AM, Zev Ross <zev at zevross.com> wrote:
> Hi All,
>
> Is there a simple way to convert a string such as c("A", "B" ,"C", "D") to a
> string of c("Group1", "Group1", "Group2", "Group2"). Naturally I could use
> the factor function as below but I don't like seeing that warning message
> (and I don't want to turn off warning messages). Perhaps a function called
> "reclassify" or "recategorize"?
>
> Zev
>
> x<-LETTERS[1:4]
> x2<-as.character(factor(x, levels=LETTERS[1:4], labels=rep(c("Group1",
> "Group2"), each=2)))
>
> Warning message:
> In `levels<-`(`*tmp*`, value = c("Group1", "Group1", "Group2", "Group2" :
>  duplicated levels will not be allowed in factors anymore

If you want to "translate", why not first build a translation table

tt = cbind(LETTERS[1:4], c("group1", "group1", "group2", "group2"))

then apply it on an example:

xx = sample(LETTERS[1:4], 20, replace = TRUE)

translation = tt[ match(xx, tt[, 1]), 2]

> translation
 [1] "group2" "group2" "group2" "group2" "group2" "group1" "group2" "group1"
 [9] "group2" "group1" "group1" "group2" "group2" "group2" "group1" "group2"
[17] "group2" "group1" "group1" "group2"

Or did I misunderstand your intent?

Peter



More information about the R-help mailing list