as.numeric()

as.numeric()

The as.numeric() function will convert a factor or character value into a numeric value. For instance, if you need to change a factor such as `Gender` 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).

Idea
Example:

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

Example output:

ouput of the head function showing 6 rows of Gender and Gen_num

    • Related Articles

    • 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 ...
    • Variables

      A variable is a measurable characteristic or attribute that can take on different values across cases (i.e., observational units such as people, companies, or time points). Types of Variables Variables can be classified by the type of values they ...
    • Data

      Data Data are recorded observations, measurements, or information collected to answer questions, test hypotheses, or make decisions. In statistics and data science, data are the raw material we analyze to discover patterns, relationships, and ...
    • Measurement

      Measurement is the process of assigning numbers or categories to variables so they can be analyzed, modeled, and used to answer research questions. Measurement is the foundation of all analysis because what you measure (and how you measure it) ...
    • Vectors

      Vectors Vectors are one of the most basic and important data structures in R. A vector is an ordered collection of values of the same data type, such as numbers, characters, or logical (TRUE/FALSE) values. In statistics and data science, vectors are ...