Open In App

How to Check Data Type in R?

Last Updated : 24 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

R programming has data types that play a very significant role in determining how data is documented, modified, and analyzed. Knowing the data type of an object is an essential part of most data analysis and programming. The current article delivers a step-by-step guide that will familiarize you with checking different data types in R, with the help of several methods and the applications of each one of them.

Checking Data Types

R Programming Language offers different functions to verify the data type of a particular object. Here are some commonly used methods:

  1. class() function
  2. type of() function
  3. str() function
  4. is.() function
  5. mode() function

Now we will discuss all these methods in detail with examples.

Checking Data Types using class() function

The class() function returns the data type of an object as a character string.

R
x <- 10
class(x)  

y <- "Hello, World!"
class(y) 

Ouput:

[1] "numeric"
[1] "character"

Checking Data Types using typeof() function

The typeof() function returns a more low-level representation of the data type compared to class().

R
x <- 10
y <- "Hello, World!"
typeof(x)  
typeof(y)  

Ouput:

[1] "double"
[1] "character"

Checking Data Types using is.() function

R provides a set of functions that start with is. to check if an object is of a specific data type. These functions return a logical value (TRUE or FALSE).

R
x <- 10
y <- "Hello, World!"
is.numeric(x)  
is.character(y)  

Ouput:

[1] TRUE

[1] TRUE

Checking Data Types using str() function

The str() function provides a compact representation of an object's internal structure, including its data type and dimensions.

R
x <- 10
y <- "Hello, World!"
str(x)  
str(y)  

Ouput:

 num 10
chr "Hello, World!"

Checking Data Types using mode() function

The mode() function is an alternative way to check the data type of an object. It returns the storage mode of the object.

R
x <- 10
y <- "Hello, World!"
mode(x)  
mode(y)  

Output:

[1] "numeric"
[1] "character"

Conclusion

Data types validation stands as the basics of objects recognition in R during data processing and programming. By observing and applying what functions and operators R has in, you can guarantee that data handling is done correctly, as well as lengthen the path to a successful involvement that will help you out with making choices over proper operations and analyses.


Next Article
Article Tags :

Similar Reads