Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conversion from factor to numeric in R

Take a factor in R, let’s say:

fac <- factor(c(2,3,4))

If I convert it to a numeric like so (edited, I originally posted the hashed out line):

#fac |> as.character(as.numeric())
fac |> as.numeric(as.character())

I get the numeric vector: c(1,2,3), which is of course wrong.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

However this isn’t the equivalent of either:

fac |> as.numeric() |> as.character()

which gives c("1","2","3"), or:

fac |> as.character() |> as.numeric()

which would give c(2,3,4)

Any help with what’s going on?

>Solution :

If you actually want to know what fac |> as.character(as.numeric()) does (as opposed to "how should I convert a factor to its numerical equivalent?", which is answered here), try the following:

> trace("as.character")
> trace("as.numeric")
> fac |> as.character(as.numeric())
trace: as.character(fac, as.numeric())
trace: as.numeric()
[1] "2" "3" "4"

i.e., here’s what’s happening:

  • call as.character() with first argument fac and second argument as.numeric()
  • as.numeric() returns numeric(0) (i.e., an empty numeric vector)
  • looking at ?as.character, it takes a ... argument which can be used to pass optional arguments to the methods for converting a particular type. As far as I can tell this is simply ignored in the case of the as.character.factor method …
  • so we end up calling the equivalent of as.character(fac) → the result is c("2", "3", "4")

In contrast, fac |> as.numeric(as.character()) (nesting in the opposite order from what was originally written in the question) evaluates as.character() (→ empty character vector) and includes it as the second argument to as.numeric(), which is again ignored, so the result is equivalent to as.numeric(fac) (which is c(1, 2, 3) as explained in various places …)

For what it’s worth, the magrittr/tidyverse pipe (%>%) gives the same results (for me).

R 4.4.0 (devel) on Linux.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading