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

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top