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.
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 argumentfacand second argumentas.numeric() as.numeric()returnsnumeric(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 theas.character.factormethod … - so we end up calling the equivalent of
as.character(fac)→ the result isc("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.