6

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
Eric Green
  • 7,385
  • 11
  • 56
  • 102
  • 1
    I think the best option would be to create another object before changing it to factor class. i.e. `v1 <- c(1,1,0,1,0,1); fact <- factor(v1, levels=c(1,0), labels=c('yes', 'no'));unique(v1)[as.numeric(fact)]` – akrun Dec 16 '15 at 21:55
  • 4
    Once you change the labels, this isn't possible (in the most general sense). There are ways you could go back in specific cases if you know what the original codes were and what you replaced them with. For instance, the solutions you tried would have worked had you not changed the labels. – joran Dec 16 '15 at 21:58
  • 1
    that's what i was missing. i thought R was storing the new information with the old. i think i wanted R to do something more like Stata with data labels. – Eric Green Dec 16 '15 at 21:59
  • 1
    e.g. `c(1,0)[as.integer(fact)]` would work in your case, but this requires that you know the original codes and their "order". – joran Dec 16 '15 at 22:01
  • [This](http://stackoverflow.com/questions/5869539/confusion-between-factor-levels-and-factor-labels) may be a relevant Q&A. – Henrik Dec 16 '15 at 22:05
  • yes, @Henrik. when i read it i did not appreciate the meaning of "But as you see, there is no label attribute." – Eric Green Dec 16 '15 at 22:07
  • I agree that Henrick's link might be relevant except that is this case this question demonstrates that the answer offered needs to be corrected. (I suspect Joris will do so, since he's a very savvy R programmer.) – IRTFM Dec 16 '15 at 22:13
  • NBATrends offered an idea that works, so I accepted the answer, but @joran clarified my confusion. thanks for the ideas. – Eric Green Dec 18 '15 at 02:07

2 Answers2

2
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"
Ven Yao
  • 3,680
  • 2
  • 27
  • 42
0

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
Raad
  • 2,675
  • 1
  • 13
  • 26
  • the important bit would be that your coding always needs to start with 0, right? – Eric Green Dec 17 '15 at 13:54
  • Can start with anything just change the constant you use to adjust. This is assuming your numbers are sequential. If they are not it gets tricky. – Raad Dec 17 '15 at 13:59