as.numeric()

as.numeric()

The as.numeric() function will convert a factorial value into a numeric value. For instance, if you need to change a factor such as `Sex` into numeric values, the as.numeric() function will assign each group a number and convert it from being coded as a categorical variable to being a quantitative variable. (See factor() for converting quantitative variables into categorical variables).

Example:

# designates `Sex` as a numeric variable and saves it as a new column called `Sex_num`
Fingers$Sex_num <- as.numeric(Fingers$Sex)
# check the data frame
head(select(Fingers, Sex, Sex_num))

Example output:
ouput of the head function showing 6 rows of Sex and Sex_num

    • Related Articles

    • numeric (in R)

      Numeric (in R) is a type of R object that is a quantitative variable.
    • factor()

      The factor() function will convert a quantitative variable into a factor (a categorical variable). This is often needed when categorical variables are dummy coded as numeric values so R treats them as a quantitative variable. For example, when a ...
    • Bar Graphs vs Histograms

      What is the difference between a bar graph and a histogram?  Bar graphs and histograms are both visualizations of data, but they visualize different types of data. Bar graphs are used for categorical variables, while histograms are used for ...
    • Histogram Bins

      Histogram bins What they are To understand the bins of a histogram, we must first understand a histogram. A histogram is a distribution of a quantitative, and often continuous, variable. A histogram divides the observations of the variable into ...