Eric Green
Eric Green

Reputation: 7725

convert factor to original numeric value

I don't know why I'm struggling with this because there seem to be numerous SO answers that address this question. But here I am.

I convert a vector of 1's and 0's to a factor and label the values "yes" and "no".

fact <- factor(c(1,1,0,1,0,1),
               levels=c(1,0),
               labels=c("yes", "no"))
#[1] yes yes no  yes no  yes
#Levels: yes no

The answers to questions about converting factors back to numeric values suggest as.numeric(as.character(x)) and as.numeric(levels(x)[x].

as.numeric(as.character(fact))
#[1] NA NA NA NA NA NA

as.numeric(levels(fact))[fact]
#[1] NA NA NA NA NA NA

Upvotes: 6

Views: 1694

Answers (2)

Raad
Raad

Reputation: 2715

The easiest solution would be to change how you specify the call to factor such that it can work with any number of numeric levels.

fact <- factor(c(1,1,0,1,0,1, 2),
               levels=c(0,1, 2),
               labels=c("no", "yes", "maybe"))
as.numeric(fact) - 1

Upvotes: 0

Ven Yao
Ven Yao

Reputation: 3710

fact <- factor(c(1,1,0,1,0,1),
               levels=c(1,0),
               labels=c("yes", "no"))
fact
# [1] yes yes no  yes no  yes
# Levels: yes no
levels(fact)
# [1] "yes" "no" 

Now, the levels of fact is a character vector. as.numeric(as.character(fact)) is in no way to do the job.

c(1, 0)[fact]
# [1] 1 1 0 1 0 1

Update:

unclass(fact)
# [1] 1 1 2 1 2 1
# attr(,"levels")
# [1] "yes" "no" 
mode(fact)
# [1] "numeric"

Upvotes: 2

Related Questions