Built with Tutify.io

    AIStudyLabs

    R Convert DataFrame Column to Numeric Type

    R Convert DataFrame Column to Numeric Type

    How to convert the DataFrame column to numeric type in R language? You can achieve this by using as.numeric() and transform() functions. In this R DataFrame article, we will discuss how to convert a dataframe column to a numeric type with examples.

    There are two ways to convert dataframe column to a numeric type. They are:

    1. Using as.numeric()
    2. Using transform() with as.numeric()

    <a id="h-1-quick-look"></a>

    1. Quick Look

    Following are quick examples.

    
    # Create dataframe with 5 rows and 3 columns
    my_dataframe=data.frame(column1=c('2','1','3','4','5'),column2=c('1','2','3','5','4'),column3=c('56','23','45','67','43'))
    
    #Display dataframe
    print(my_dataframe)
    
    #Return the datatype of each column
    print(sapply(my_dataframe, class)) 
    
    #Convert column1 column type to numeric
    my_dataframe$column1 = as.numeric(as.character(my_dataframe$column1)) 
    
    #Return the datatype of each column
    print(sapply(my_dataframe, class)) 
    
    #Return the datatype of each column by converting column to numeric with transform()
    print(sapply(transform(my_dataframe,column1 = as.numeric(column1)), class))
    

    Let's create an R dataframe with 5 rows and 3 columns.

    
    # Create dataframe with 5 rows and 3 columns
    my_dataframe=data.frame(column1=c('2','1','3','4','5'),column2=c('1','2','3','5','4'),
    column3=c('56','23','45','67','43'))
    
    #Display dataframe
    print(my_dataframe)
    
    #Return the datatype of each column
    print(sapply(my_dataframe, class))
    

    Output:

    
    # Output
      column1 column2 column3
    1       2       1      56
    2       1       2      23
    3       3       3      45
    4       4       5      67
    5       5       4      43
    
    # Data types 
        column1     column2     column3 
    "character" "character" "character"
    

    We can see that all the column datatypes are of character type, we can get the data type of each column in the dataframe using the sapply() function. Now, let's see how to convert the dataframe column to a numeric type.

    <a id="h-1-convert-dataframe-column-to-numeric-type-using-as-numeric"></a>

    1. Convert DataFrame Column to Numeric Type using as.numeric()

    as.numeric() is used to convert the R dataframe (data.frame) column to the numeric type from the character type. Here, it will take the column name in the form of a character type.

    Syntax:

    
    # Syntax
    my_dataframe$column = as.numeric(as.character(my_dataframe$column))
    

    where my_dataframe is the input dataframe and the column refers to the column name.

    Example:

    In this example, we will convert column1 to a numeric type.

    
    #convert column1 column type to numeric
    my_dataframe$column1 = as.numeric(as.character(my_dataframe$column1)) 
    
    #return the datatype of each column
    print(sapply(my_dataframe, class))
    

    Output:

    
     column1     column2     column3 
      "numeric" "character" "character"
    

    From the above code, we can see that column1 is converted to a numeric type.

    <a id="h-2-convert-dataframe-column-to-numeric-type-using-transform-with-as-numeric"></a>

    2. Convert DataFrame Column to Numeric Type using transform() with as.numeric()

    transform() will take two parameters. the first parameter is the dataframe input and the second parameter takes as.numeric() method which will convert the specified column to numeric

    Syntax:

    
    transform(my_dataframe,column1 = as.numeric(column))
    

    where my_dataframe is the input dataframe and the column refers to the column name.

    Example:

    In this example, we will convert column1 to a numeric type and use sapply() function to get the data types for columns.

    
    #return the datatype of each column by converting column to numeric with transform()
    print(sapply(transform(my_dataframe,column1 = as.numeric(column1)), class))
    

    Output:

    
     column1     column2     column3 
      "numeric" "character" "character"
    

    From the above code, we can see that column1 is converted to a numeric type.

    <a id="h-conclusion"></a>

    Conclusion

    From the above article, we saw how to convert an R dataframe column to a Numeric Type using as.numeric() and transform() with as.numeric() methods.

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References

    1. as.numeric() in R
    2. transform()