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 levels
attribute. This attribute appears on the console when the dados$x
is printed.
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 dados$x
are extracted and numbered. R
automatically places 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))
, though it may be harder to remember.