Hi
I need to convert factors to quantitative variables. What is the best method to use in R please?
Many Thanks
Hi
I need to convert factors to quantitative variables. What is the best method to use in R please?
Many Thanks
Just convert the factor to a number:
f <- factor(c("c", "a", "b"))
as.numeric(f)
If you have numeric items in a vector that is stored as a factor, it is a bit problematic, but you can get the numbers back.
f <- factor(c(17, 9, 27, 36)) # make a factor
as.numeric(f)
#> [1] 2 1 3 4
# that does not work right - provides levels, not the actual numbers
as.character(f) # recovers labels - but as character strings
#> [1] "17" "9" "27" "36"
as.numeric(as.character(f)) # converts to numeric
#> [1] 17 9 27 36
Created on 2019-11-12 by the reprex package (v0.3.0)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.