[R] Recoding lists of categories of a variable

peter dalgaard pdalgd at gmail.com
Tue Oct 11 11:59:14 CEST 2016


On 11 Oct 2016, at 01:32 , S Ellison <S.Ellison at LGCGroup.com> wrote:

>> Well, I think that's kind of overkill.
> Depends whether you want to recode all or some, and how robust you want the answer to be. 
> recode() allows you to recode a few levels of many, without dependence on level ordering; that's kind of neat. 
> 
> tbh, though,  I don't use recode() a lot; I generally find myself need to change a fair proportion of level labels. 
> 
> But I do get nervous about relying on specific ordering; it can break without visible warning if the data change (eg if you lose a factor level with a slightly different data set, integer indexing will give you apparently valid reassignment to the wrong new codes).  So I tend to go via named vectors even if it costs me a lot of typing. For example to change 
> lcase<-c('a', 'b', 'c') 
> 
> to c('B', 'A', 'C') I'll use something like 
> 
> c(a='B', b='A', c='C')[lcase] 
> 
> or, if lcase were a factor, 
> c(a='B', b='A', c='C')[as.character(lcase)] 

Notice that similar functionality is available via levels<-() (see help page for more features)

> f <- factor(c("a","b","c"))
> levels(f) <- list(A="a", B="b", C="c")
> f
[1] A B C
Levels: A B C

The main advantage of this is that you control the level ordering, and also that you don't quite as easily get caught out by unused levels:

> f <- factor(c("a","c"))
> levels(f) <- list(A="a", B="b", C="c")
> table(f)
f
A B C 
1 0 1 

(in which the 0 count might be important).

-pd

> 
> Unlike using the numeric levels, that doesn't fail if some of the levels I expect are absent; it only fails (and does so visibly) when there's a value in there that I haven't assigned a coding to. So it's a tad more robust.
> 
> Steve E
> 
> 
> 
> 
> 
> 
> *******************************************************************
> This email and any attachments are confidential. Any use...{{dropped:8}}
> 
> ______________________________________________
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list