2
a <- factor(2:4, labels=c("a", "b", "c"))
a
## [1] a b c
## Levels: a b c

as.numeric(a)
## [1] 1 2 3

I want to drop labels and get the numeric values 2, 3, 4, but the result of as.numeric(a) is 1 2 3. Don't tell me the answer is as.numeric(a) + 1. Thank you.

Jaap
  • 81,064
  • 34
  • 182
  • 193
stata
  • 513
  • 2
  • 6
  • 12
  • See here: http://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-an-integer-numeric-without-a-loss-of-information – John Paul May 19 '14 at 14:13
  • I have read this post,but can not solve my question. So I posted my question. – stata May 19 '14 at 14:15
  • This appears to be slightly more complicated than the linked question. Due to the relabelling, the accepted answer doesn't work. – MattLBeck May 19 '14 at 14:18
  • 4
    Well, this question as posed it actually impossible. Once you specify the labels argument as the OP did, the original values of 2, 3 and 4 are completely lost. They are gone forever and not recoverable (in a general sense). – joran May 19 '14 at 14:23
  • @joran which is the correct answer for this question, instead of marking this as a duplicate. – MattLBeck May 19 '14 at 14:23
  • 1
    @Mattrition relax - I reopened it. – Simon O'Hanlon May 19 '14 at 14:23
  • @Mattrition Calm down...I was taking my time to make sure before re-opening it as well (just as Simon was...). – joran May 19 '14 at 14:24
  • @zx8754 yes. The `1 2 3` refer to the three levels, `a b c`. – Simon O'Hanlon May 19 '14 at 14:25
  • @zx8754 Yes. The underlying integer codes are created via `match(x,levels)`, so any connection to the original values passed has been eliminated. – joran May 19 '14 at 14:25
  • @joran, Stata and SAS can drop value labels. Why R do not? – stata May 19 '14 at 14:26
  • 2
    If the underlying values are important, then `factor` is the wrong choice of data structure. The point of factors is that all that is important is the labels. Factors are always stored as (arbitrary) integers codes mapping to the labels. – joran May 19 '14 at 14:27
  • 2
    @Stata perhaps if you showed us the context in which you want to do this we can suggest an alternative method. – MattLBeck May 19 '14 at 14:27
  • This is a legitimate question. The `factor` function is confusing. The `levels` is input, and the `labels` is the output. See http://stackoverflow.com/questions/5869539/confusion-between-factor-levels-and-factor-labels – Feng Jiang Feb 27 '17 at 22:07

3 Answers3

4

Since you haven't provided any broader context for why you're attempting this, what follows is simply a guess at something that might be useful:

a<-factor(2:4, labels=c("a", "b", "c"))
> names(a) <- as.character(2:4)
> a
2 3 4 
a b c 
Levels: a b c
> as.integer(names(a))
[1] 2 3 4

I can't say that I've ever used a "named factor" before, and frankly the concept feels a little odd to me.

If you don't really want a factor, then simply doing:

a <- 2:4
names(a) <- letters[1:3]

would probably suffice.

joran
  • 169,992
  • 32
  • 429
  • 468
1

When you want to change the levels of the factor, just use:

levels(a) <- c("a"=2,"b"=3,"c"=4)

which gives:

> a
[1] 2 3 4
Levels: 2 3 4

As @joran showed in his answer, using names works as well. However there is no specific need to use as.character in my opinion:

names(a) <- 2:4

which gives:

> a
2 3 4 
a b c 
Levels: a b c

Additionally, you can use as.integer as @joran showed.


With the names argument you assign names to values. The levels argument is only used for factor variables.

For example, when you set your vector like:

a <- 2:4
names(a) <- letters[1:3]

using levels(a), gives: NULL

When you set your vector like:

a <- 2:4
levels(a) <- c("ab","bc","cd")

using names(a), gives: NULL

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • Redefine levels is a solution. But if many factors like a to redefine, it is terrible. In Stata,command "label drop _all" can drop all value labels. – stata May 19 '14 at 14:38
  • 1
    @stata why do you want to drop the factor levels? – Jaap May 19 '14 at 14:43
  • Stata and SAS can do it, So I want to know how to do it in R. – stata May 19 '14 at 14:57
  • 2
    @stata Just so you know, when several people are attempting to help you, and they are all asking some version of "can you give more background on what you're trying to do?" and the only response we get is "SAS and Stata can do it, why can't R?" you're much more likely to simply piss people off than actually get a useful answer. It comes across as really whiny, ungrateful and demanding, which I'm sure is not your intent. – joran May 19 '14 at 15:07
  • @joran Thanks for everyone. I'm very intersting with Stata,SAS and R. I had completed SAS macros and Stata ado files. Recently I would write R functions. When I added label to a factor today, I think that if how to drop labels and preserve underlying values in dataframe. So I posted my question. More parctice, more think. – stata May 19 '14 at 15:17
  • @stata why do you want to drop the labels in the first place? – MattLBeck May 19 '14 at 15:47
0

I wrote a package called "lfactors" that you may appreciate here

a <- lfactor(2:4,levels=2:4, labels=c("a", "b", "c"))
a
# [1] a b c
# Levels: a b c
# Numeric levels: 2 3 4 
as.numeric(a)
# [1] 2 3 4

it has other features you may also like

a==3
# [1] FALSE  TRUE FALSE
a=="b"
# [1] FALSE  TRUE FALSE
pdb
  • 1,574
  • 12
  • 26