Skip to content
Back to formatted view

Raw Message

Message-ID: <515078BB.8010002@gmail.com>
Date: 2013-03-25T16:18:03Z
From: Duncan Murdoch
Subject: Obtaining the internal integer codes of a factor XXXX
In-Reply-To: <CAPRGo-=Fm=GM5v8MD2VnYKQwPpwA6fHF1JRr42Fd1z1ZRFXKuw@mail.gmail.com>

On 25/03/2013 11:37 AM, Dan Abner wrote:
> Hi everyone,
>
> I understand the process of obtaining the internal integer codes for
> the raw values of a factor (using as.numeric() as below), but what is
> the best way to obtain these codes for the levels() of a factor (since
> the as.numeric() results don't really make clear which code maps to
> which level)?
>
>
> fdata<-factor(c("b","b","c","a","b","c"),labels=c("I","II","III"))
> fdata
> levels(fdata)
>
> as.numeric(fdata)
>
>
> I thought something like this would make sense and work, but it throws an error:
>
> as.numeric(levels(fdata))

seq_len(length(levels(fdata)))

will give you the numeric codes for the levels.  They are simply 1:3 in 
your example above.  (Or perhaps I misunderstood your question?)

Duncan Murdoch