How to check data type in R?

This recipe will explain different functions to check the data type in R through examples.

Recipe Objective: How to Check Datatype in R?

This step-by-step recipe will show you how to find data type in R.

How to Check Variable Type in R?

There are several ways in R to check variable type. We can make use of the “typeof()” function, “class()” function, and even the “str()” function to check the data type of an entire dataframe. Apart from these, we can even use “is.datatype()” function (where datatype could be character, numeric, integer, complex, or logical) to check variable type R –

  1. is.numeric() to check if the  data is numeric

  2. is.character() to check if the data is character type

  3. is.integer() to check if the data is an integer

  4. is.complex() to check if the data is complex

  5. is.logical() to check if the data holds a logical value i.e. TRUE or FALSE

Let us check out how these functions work with the help of examples. We will store data of different types in different variables to demonstrate these functions.

Code:
a <- 1
b <- 2.4
c <- "Set of characters"
d <- TRUE
i <- as.integer(a)
 How to Check Variable Type in R

R- Check Type of Variable using typeof()

First, let us see how to use the “typeof()” function to check the data type in R.

Code:
#checking data type
print(typeof(a))
print(typeof(b))
print(typeof(c))
print(typeof(d))
print(typeof(i))
 R- Check Type of Variable using typeof()

 

Output:

[1] "double"

[1] "double"

[1] "character"

[1] "logical"

[1] "integer"

R- Check Data Type Using class()

Next, we will see how to use the “class()” function to check the data type.

Code:
#checking data type
print(class(a))
print(class(b))
print(class(c))
print(class(d))
print(class(i))
 R- Check Data Type Using class()

 

Output:

[1] "numeric"

[1] "numeric"

[1] "character"

[1] "logical"

[1] "integer"

R- Get Type of Variable Using is.datatype()

Let us check the working of the “is.datatype()” function.

Code:
#checking data type
print(is.numeric(a))
print(is.complex(b))
print(is.character(c))
print(is.logical(d))
print(is.integer(i))
 R- Get Type of Variable Using is.datatype()

 

Output:

[1] TRUE

[1] FALSE

[1] TRUE

[1] TRUE

[1] TRUE

R- Get Data Type Using str()

To demonstrate the “str()” function, we will attach the mtcars dataset and check the data type of each column.

Code:
#attaching mtcars dataset
attach(mtcars)

#checking data structure including the datatypes of columns
str(mtcars)
 R- Get Data Type Using str()

 

Output:

'data.frame':    32 obs. of  11 variables:

 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...

 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...

 $ disp: num  160 160 108 258 360 ...

 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...

 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...

 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...

 $ qsec: num  16.5 17 18.6 19.4 17 ...

 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...

 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...

 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...

 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

We can see that all the columns of the mtcars dataset are of numeric type.

 

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

House Price Prediction Project using Machine Learning in Python
Use the Zillow Zestimate Dataset to build a machine learning model for house price prediction.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP