Convert factor to character
In R, you used sometimes functions to convert vectors to another
format with as.character
, as.factor
or as.numeric
.
However, you need to be careful when using the function as.numeric
on
a factor. Indeed, if the factor contains factor number, it will
convert them to the level number and not convert the writing number
from your factor.
Here is a short example:
[1] "22.3072855733335" "33.1896589370444" "42.3720560967922" "57.6603659661487" [5] "71.072455169633" "81.9448956055567" "84.052187949419" "96.3544549420476" [9] "97.8130409261212" "99.6611237060279"
factor_to_convert | using_as_numeric_only | using_as_character_first |
---|---|---|
57.6603659661487 | 4 | 57.6603659661487 |
22.3072855733335 | 1 | 22.3072855733335 |
33.1896589370444 | 2 | 33.1896589370444 |
71.072455169633 | 5 | 71.072455169633 |
81.9448956055567 | 6 | 81.9448956055567 |
42.3720560967922 | 3 | 42.3720560967922 |
96.3544549420476 | 8 | 96.3544549420476 |
97.8130409261212 | 9 | 97.8130409261212 |
84.052187949419 | 7 | 84.052187949419 |
99.6611237060279 | 10 | 99.6611237060279 |
So, be careful when you are using factor with numerical levels and you want to convert them.