Treat Discrete Variable as Continuous Data in R (Example Code)
In this article you’ll learn how to set a discrete variable to a continuous variable in the R programming language.
Creation of Example Data
my_fac <- factor(1:5) # Construct example variable my_fac # Display example variable # [1] 1 2 3 4 5 # Levels: 1 2 3 4 5 |
my_fac <- factor(1:5) # Construct example variable my_fac # Display example variable # [1] 1 2 3 4 5 # Levels: 1 2 3 4 5
Example: Convert Factor to Numeric Using as.character() & as.numeric() Functions
my_num <- as.numeric(as.character(my_fac)) # Convert categorical to numeric my_num # Display updated variable # [1] 1 2 3 4 5 |
my_num <- as.numeric(as.character(my_fac)) # Convert categorical to numeric my_num # Display updated variable # [1] 1 2 3 4 5