[R] Recoding lists of categories of a variable
S Ellison
S.Ellison at LGCGroup.com
Tue Oct 11 01:32:08 CEST 2016
> 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)]
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}}
More information about the R-help
mailing list