Table of Contents
The process of converting a column in a DataFrame to a numeric type involves using the appropriate method or function to change the data type of the values within the column from their current format to a numerical format. This can be achieved in various ways, such as using the “astype()” function or the “to_numeric()” method, depending on the programming language or software being used. By converting a column to a numeric type, the data can be manipulated and analyzed more accurately and efficiently for various purposes.
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:
- Using as.numeric()
- Using transform() with as.numeric()
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.
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.
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.
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.
Related Articles
References
Cite this article
stats writer (2024). How can I convert a column in a DataFrame to a numeric type?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-column-in-a-dataframe-to-a-numeric-type/
stats writer. "How can I convert a column in a DataFrame to a numeric type?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-column-in-a-dataframe-to-a-numeric-type/.
stats writer. "How can I convert a column in a DataFrame to a numeric type?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-column-in-a-dataframe-to-a-numeric-type/.
stats writer (2024) 'How can I convert a column in a DataFrame to a numeric type?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-column-in-a-dataframe-to-a-numeric-type/.
[1] stats writer, "How can I convert a column in a DataFrame to a numeric type?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I convert a column in a DataFrame to a numeric type?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
