67

I am manipulating a data frame using the reshape package. When using the melt function, it factorizes my value column, which is a problem because a subset of those values are integers that I want to be able to perform operations on.

Does anyone know of a way to coerce a factor into an integer? Using as.character() will convert it to the correct character, but then I cannot immediately perform an operation on it, and as.integer() or as.numeric() will convert it to the number that system is storing that factor as, which is not helpful.

Thank you!

Jeff

Jaap
  • 81,064
  • 34
  • 182
  • 193
Jeff Erickson
  • 3,783
  • 8
  • 36
  • 43
  • 2
    This is similar to http://stackoverflow.com/questions/3418128/r-how-to-convert-a-factor-to-an-integer-numeric-in-r-without-a-loss-of-informat – Aaron left Stack Overflow Jan 25 '11 at 21:20
  • This Q is NOT similar to the duplicate one. Many many people are getting NAs from the top answer (see comments). The answer that works in that post is in the middle (https://stackoverflow.com/a/42399425) but is stated as not belonging to the post. I suggest it is added to this post. So much wasted time for such a simple issue. – luchonacho Dec 23 '21 at 11:54
  • 1
    @luchonacho if NAs are a key issue for this question, that should be mentioned in the question. I don't see "many many people," I see a few comments about either non-numeric values that weren't mentioned, or entirely different use-cases like the answer you linked to. If you want a new question that's focused on handling NAs and/or non-numeric strings, you should post one – camille Dec 23 '21 at 16:11

2 Answers2

80

Quoting directly from the help page for factor:

To transform a factor f to its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)).

Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
68

You can combine the two functions; coerce to characters thence to numerics:

> fac <- factor(c("1","2","1","2"))
> as.numeric(as.character(fac))
[1] 1 2 1 2
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • I tried this, but I get the warning message: "NAs introduced by coercion." What does this mean? – Jeff Erickson Jan 25 '11 at 20:24
  • Or does this just mean that some of them were not numbers to start with? – Jeff Erickson Jan 25 '11 at 20:27
  • 1
    @Jeff it means that some of the characters aren't numbers, so they get converted to `NA` when you use `as.numeric(....)`. Look at `levels(fac)` and `as.numeric(levels(fac))` replacing `fac` with your factor variable to see which are being coerced to `NA`. – Gavin Simpson Jan 25 '11 at 21:02
  • you can use convert() from hablar to do this. `df %>% convert(num(column_name))` to safely coerce from factor to numerical – davsjob Nov 01 '18 at 09:48