If you analyze the structure of the object you will see where the problem occurs:
str(unclass(dados$x))
atomic [1:4] 2 1 4 3
- attr(*, "levels")= chr [1:4] "10" "11" "15" "20"
The object dados$x
is composed of the vector [2,1,4,3] with the attribute levels
. This attribute appears on the console when the dados$x
.
To solve the problem, in addition to the solution already mentioned, you can adopt the following solution:
as.numeric(levels(dados$x))[dados$x]
In the first part of the solution the attributes of the object are extracted and converted into number dados$x
. The R
automatically puts these values in ascending order. Then you use [dados$x]
to leave them in the original order.
This solution is slightly more efficient than as.numeric(as.character(dados$x))
, however it may be harder to remember.