Do you think IIT Guwahati certified course can help you in your career?
No
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.
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.
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.
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
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
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
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
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
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
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.
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
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.
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.