[R] Recoding lists of categories of a variable

S Ellison S.Ellison at LGCGroup.com
Tue Oct 11 18:19:06 CEST 2016


> If you are concerned about missing levels -- which I agree is legitimate -- then
> the following simple modification works (for
> **factors** of course):
> 
> > d <- factor(letters[1:2],levels= letters[1:3]) d
> [1] a b
> Levels: a b c
> > f <- factor(d,levels = levels(d), labels = LETTERS[3:1]) f
> [1] C B
> Levels: C B A
> 
> ## No levels lost !
> 
> Does that allay your concerns?

If you control the factor creation, sure. But it gets a bit untidy if your factors are created on data reading or similar; read.table doesn’t give you the option to set levels on factor creation. You have to go back over your data frame etc to fix it. That's legit, of course, but would amount to recoding the same factor twice - once to re-set the levels to the full set, and a second time to recode them.

Hardly a showstopper though; we're in timtowdi territory here and we're allowed a bit of personal preference. My preference just happens to be conditioned by SQL and other applications where ordering is explicitly _not_ guaranteed unless you specify it. So relying on implicit ordering makes me decidedly nervous, and I see associative arrays and other order-independent operations as  much safer. 

S




*******************************************************************
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If 
you have received this message in error, please notify the sender 
immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com 
and delete this message and any copies from your computer and network. 
LGC Limited. Registered in England 2991879. 
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK


More information about the R-help mailing list