R

R – Convert Data Frame Column to Numeric Type

In this R tutorial, we will discuss how to convert a data frame column to a numeric type.

Let’s create a data frame named market.

In R programming, if you want to return the data type of all columns in a data frame, then you can use the sapply() method.

Syntax:

sapply(dataframe_object,class)

Parameters:

It takes two parameters.

  1. dataframe_object refers to the data frame.
  2. Class is the method that returns the type.
#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c('1','2','3','4'),market_name=c('M1','M2','M3','M4'),
market_place=c('India','USA','India','Australia'),market_type=c('grocery','bar','grocery',
'restaurent'),market_squarefeet=c('120','342','220','110'))

#display the market dataframe

print(market)

#return all column data types

print(sapply(market,class))

Result:

You can see the market data frame with each column data type here:

Note that all the column types are character types.

Now, we need to convert some columns to numeric types.

Method 1: as.numeric()

The as.numeric() method converts the given character type column into numeric. It takes the column name as a parameter.

Syntax:

as.numeric(dataframe_object$column)

Where,

dataframe_object is the input data frame, and the column is the character type column converted to numeric.

Example 1

In this example, we convert the market_id column to numeric and display all column types using the sapply() method.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c('1','2','3','4'),market_name=c('M1','M2','M3','M4'),
market_place=c('India','USA','India','Australia'),market_type=c('grocery','bar','grocery',
'restaurent'),market_squarefeet=c('120','342','220','110'))

#convert market_id column to numeric

market$market_id=as.numeric(market$market_id)

#return all column datatypes

print(sapply(market,class))

Result:

We can observe that the market_id column is converted into numeric.

Example 2

In this example, we convert the market_squarefeet column to numeric and display all column types using the sapply() method.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c('1','2','3','4'),market_name=c('M1','M2','M3','M4'),
market_place=c('India','USA','India','Australia'),market_type=c('grocery','bar','grocery',
'restaurent'),market_squarefeet=c('120','342','220','110'))

#convert market_squarefeet column to numeric

market$market_squarefeet=as.numeric(market$market_squarefeet)

#return all column data types

print(sapply(market,class))

Result:

We can observe that the market_squarefeet column is converted into numeric.

Method 2: transform() With as.numeric()

The transform() method changes the data type of a column. It takes two parameters. The first parameter is the data frame object, and the second parameter is the column that takes as.numeric(), which is used to convert the given character type column into numeric. It takes column name as a parameter

Syntax:

transform(dataframe_object,column = as.numeric(column))

Parameters:

  1. dataframe_object is the input data frame.
  2. column is the character type column converted to numeric using as.numeric().

Example 1

In this example, we convert the market_id column to numeric and display all column types using the sapply() method.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c('1','2','3','4'),market_name=c('M1','M2','M3','M4'),
market_place=c('India','USA','India','Australia'),market_type=c('grocery','bar','grocery',
'restaurent'),market_squarefeet=c('120','342','220','110'))

#convert market_id column to numeric

market=transform(market,market_id = as.numeric(market_id))

#return all column datatypes

print(sapply(market,class))

Result:

We can observe that the market_id column is converted into numeric.

Example 2

In this example, we convert the market_squarefeet column to numeric and display all column types using the sapply() method.

#create a dataframe-market that has 4 rows and 5 columns.

market=data.frame(market_id=c('1','2','3','4'),market_name=c('M1','M2','M3','M4'),
market_place=c('India','USA','India','Australia'),market_type=c('grocery','bar','grocery',
'restaurent'),market_squarefeet=c('120','342','220','110'))

#convert market_squarefeet column to numeric

market=transform(market,market_squarefeet = as.numeric(market_squarefeet))

#return all column data types

print(sapply(market,class))

Result:

We can observe that the market_squarefeet column is converted into numeric.

Conclusion

From this article, we saw two methods to convert a data frame column to numeric type using as.numeric() and transform() methods. We learned that transform() used as.numeric() to convert a column to a numeric type.

About the author

Sireesha Lavu

This is Sireesha Lavu from Gogulamudi, Andhra Pradesh, India 522015.
I am currently working as a teacher and interested in writing technical articles on computer science.