Table of contents
1.
Introduction
2.
What are R Data types?
3.
Data Types in R Programming Language
3.1.
Numeric Data Type
3.1.1.
Code 
3.1.2.
Output 
3.2.
Integer Data Type
3.2.1.
Code 
3.2.2.
Output 
3.2.3.
Code 
3.2.4.
Output 
3.3.
Complex Data Type
3.3.1.
Code 
3.3.2.
Output 
3.4.
Logical Data Type
3.4.1.
Code 
3.4.2.
Output 
3.5.
Character Data Type
3.5.1.
Code 
3.5.2.
Output 
3.5.3.
Code 
3.5.4.
Output 
3.6.
Raw Data Type
3.6.1.
Code
3.6.2.
Output 
4.
Coerce the Data Type of an Object to Another
5.
Find Data Type of an Object in R
6.
Coerce or Convert the Data Type of an Object to Another
6.1.
Explanation
7.
Frequently Asked Questions
7.1.
What are the 4 data structures in R?
7.2.
How to find data type in R?
7.3.
How to type in data in R?
8.
Conclusion
Last Updated: Nov 26, 2024
Medium

Data Types in R Programming

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

When it comes to working with data in R, understanding data types is fundamental. Just like in real life, where you have different types of objects like numbers, words, and lists, R also categorizes data into various types. These data types help R understand how to handle and manipulate your data.

data types in r programming

There are numerous data types in R programming language, including integer, string, and more. Based on the variable's data type, the operating system allows memory and determines what can be kept in the reserved memory. This article will discuss all the data types in R Programming Language so keep reading the blog. 

Also see, Must Do Coding Questions

What are R Data types?

In R, data types represent the kind of data that can be stored and manipulated. Common data types include numeric (for numbers), character (for text), logical (for true/false values), and factors (for categorical data). R also supports more specialized types like lists and data frames for structured data storage. These data types help organize and work with diverse data in R programming.

Also See, resume for software engineer fresher

Data Types in R Programming Language

There are the following data types in R Programming Language.

Data Types in R Programming Language

 

Data TypeValuesExamples
NumericReal numbers3.14, -42, 0.5
CharacterText"Hello, World!", "R Programming"
LogicalTrue or FalseTRUE, FALSE
IntegerWhole numbers42L, -10L
ComplexComplex numbers3 + 2i, -1 - 4i
FactorCategorical datafactor("Low", "Medium", "High")
DateDate and timeas.Date("2023-09-28"), as.Date("1985-12-25")
DateTimeDate and time with time zoneas.POSIXct("2023-09-28 15:30:00 PST"), as.POSIXct("2023-09-28 09:00:00 UTC")

Numeric Data Type

For numerical values, there is the numeric data type. It is a standard data type in r programming for numbers.

The following are some examples of numerical values: 2, 25.4, -45.253, 48.02, etc.

Code 

num <- -250
class(num)
typeof(num)

Output 

Output

Note: R transforms a number into a "double" or a decimal type with a minimum of two decimal places before storing it in a variable. As a result, a value like "10" is stored as "10.00" with the type "double" and the class "numeric." 

Integer Data Type

The Integer data type represents integer values. We must specify a value as an integer to store it as one. The integer data type is frequently employed for discrete-only values like unique IDs. The as.integer() function allows us to store and convert a value into an integer type. 

Code 

 int <- as.integer(16)
 class(int)
 typeof(int)
 int2 <- as.integer(20)
 class(int2)
 typeof(int2)

Output 

Output

Note: To indicate that a specific value is of the integer data type, we can alternatively use the capital "L" notation.

Code 

 int <- 16L
 class(int)
 typeof(int) 

Output 

Output

Complex Data Type

Complex data types in r programming are used to hold numbers that have fictitious components. Complex values include things like 2+3i, 1i, 5-4i, etc. 

Code 

comp <- 22-6i
class(comp)
typeof(comp)

Output 

Ouput

Logical Data Type

Logical data types in R programming accept either a true or false value. It is common to compare two variables to arrive at a logical value. 

Code 

logi <- FALSE
class(logi)
typeof(logi)

Output 

Output

Character Data Type

Character data types in r programming are used to hold strings or character values. R allows for using symbols, numbers, and the alphabet in strings. Wrapping data in one or two inverted commas is the easiest way to indicate in R that it is of the character type. 

Code 

char <- "dataflair1234"
class(char)
typeof(char)

Output 

Output


Note: The as.character() function can also be used to convert a value to the character data type or to store it as a character.

Code 

char2 <- as.character("helloNinja")
char2
class(char2)
typeof(char2)

Output 

Output

Raw Data Type

Values are specified as raw bytes in a raw data type. The methods listed below can change a character data type into a raw one and vice versa. 

  • charToRaw() - converts character data to raw data
     
  • rawToChar() - converts raw data to character data
     

Code

# convert character to raw byte
rawVariable <- charToRaw("Welcome to Coding Ninjas")

print(rawVariable)
print(class(rawVariable))

# convert raw bytes to character
charVariable <- rawToChar(rawVariable)

print(charVariable)
print(class(charVariable))

Output 

Output

Coerce the Data Type of an Object to Another

In R, you can convert or "coerce" the data type of an object to another type using various functions like as.numeric(), as.character(), as.logical(), and so on. This process allows you to change the type of data stored in an object to suit your needs. For example, if you have a character string representing numbers, you can coerce it to numeric type for mathematical operations. Similarly, you can convert numeric values to character strings or logical values as required. Coercion is a powerful feature in R that enables flexible data manipulation and analysis.

You can also read about mock interview.

Find Data Type of an Object in R

To find the data type of an object in R, use the class() or typeof() function.

  • class(object): Returns the object's class (e.g., "numeric," "character").
     
  • typeof(object): Returns the underlying type of the object (e.g., "double," "integer").

Example:

x <- 42
class(x)    # Output: "numeric"
typeof(x)   # Output: "double"

Explanation

The class() function gives the high-level classification of the object, while typeof() provides its internal storage type. Together, they help understand and debug object behavior.

Coerce or Convert the Data Type of an Object to Another

In R, you can coerce an object's data type using coercion functions like as.numeric(), as.character(), as.factor(), etc.

Example:

x <- "42"          # x is a character
y <- as.numeric(x) # y is now numeric

Explanation

Coercion is the process of converting one data type to another, allowing compatibility with specific functions or analyses. R provides various as.*() functions for explicit type conversion. Use with care, as invalid coercions (e.g., as.numeric("text")) can lead to warnings or NA values.

Frequently Asked Questions

What are the 4 data structures in R?

The four main data structures in R are vectors, matrices, lists, and data frames. These structures organize and store data in different ways, providing flexibility for various analytical tasks.

How to find data type in R?

Use class(object) to determine the class (e.g., "numeric") or typeof(object) to find the internal type (e.g., "double") of an object.

How to type in data in R?

Data can be entered using functions like c() for vectors, matrix() for matrices, and data.frame() or tibble() for structured data.

Conclusion

In this article, we discussed the data types in r programming. Understanding R data types lays the foundation for effective data manipulation and analysis. From numeric and character to factors and lists, each data type serves a unique purpose in R programming.

To study more about data types, refer to Abstract Data Types in C++ and Data Types in C++.

Also check out the Interview guide for Product Based Companies as well as some of the Popular interview problems from top tech companies like Amazon, Adobe, Google, Uber, Microsoft, etc.

Refer to our guided pathways on Code studio to learn more about Competitive ProgrammingJavaScriptSystem DesignDSA C++etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the Top 150 Interview Puzzlesinterview experiences and interview package.

Live masterclass