Data Types in R
In this tutorial, we will learn about data types in R. We will cover the basics of different data types available in R, including numeric, integer, character, logical, and complex types, as well as how to work with them.
Understanding Data Types in R
R supports several basic data types that are used to store different kinds of values. Knowing these data types is fundamental to working with R.
Numeric Data Type
The numeric data type in R is used to store both integer and floating-point numbers. By default, numbers in R are stored as double precision floating-point numbers.
num <- 42.5
print(num)
class(num)Integer Data Type
The integer data type is used to store integer values. To explicitly create an integer, append an L to the number.
int <- 42L
print(int)
class(int)Character Data Type
The character data type is used to store text or string values. Character strings are enclosed in either single or double quotes.
char <- "Hello, R!"
print(char)
class(char)Logical Data Type
The logical data type is used to store boolean values: TRUE or FALSE.
flag <- TRUE
print(flag)
class(flag)Complex Data Type
The complex data type is used to store complex numbers.
comp <- 3 + 4i
print(comp)
class(comp)Raw Data Type
The raw data type is used to store raw byte data. It is commonly used for storing binary data or data that does not fit into other data types.
raw_data <- as.raw(c(0x41, 0x42, 0x43))
print(raw_data)
class(raw_data)Example 1: Working with Numeric Data Type
In R, working with numeric data types involves performing arithmetic operations, statistical calculations, and using various built-in functions to manipulate and analyze numeric values.
For example,
- Create a numeric variable named
numand assign it a floating-point value of 42.5. - Print the value of
numto the console. - Use the
classfunction to determine the data type ofnumand print the result.
R Program
num <- 42.5
print(num)
print(class(num))Output
[1] 42.5 [1] "numeric"
Example 2: Working with Integer Data Type
In R, working with integer data types involves handling whole numbers, performing arithmetic operations, and using specific functions to ensure data is treated as integers for precise calculations.
For example,
- Create an integer variable named
intand assign it an integer value of 42 using theLsuffix. - Print the value of
intto the console. - Use the
classfunction to determine the data type ofintand print the result.
R Program
int <- 42L
print(int)
print(class(int))Output
[1] 42 [1] "integer"
Example 3: Working with Character Data Type
In R, working with character data types involves manipulating strings, performing text operations, and utilizing functions like paste, substr, and gsub for various text processing tasks.
For example,
- Create a character variable named
charand assign it a string value of "Hello, R!". - Print the value of
charto the console. - Use the
classfunction to determine the data type ofcharand print the result.
R Program
char <- "Hello, R!"
print(char)
print(class(char))Output
[1] "Hello, R!" [1] "character"
Example 4: Working with Logical Data Type
In R, working with logical data types involves handling TRUE and FALSE values, performing logical operations, and using them in control structures like if statements and loops.
For example,
- Create a logical variable named
flagand assign it a value ofTRUE. - Print the value of
flagto the console. - Use the
classfunction to determine the data type offlagand print the result.
R Program
flag <- TRUE
print(flag)
print(class(flag))Output
[1] TRUE [1] "logical"
Example 5: Working with Complex Data Type
In R, working with complex data types involves handling numbers with real and imaginary parts, performing arithmetic operations, and using functions like Re, Im, and Mod to manipulate them.
For example,
- Create a complex variable named
compand assign it a value of 3 + 4i. - Print the value of
compto the console. - Use the
classfunction to determine the data type ofcompand print the result.
R Program
comp <- 3 + 4i
print(comp)
print(class(comp))Output
[1] 3+4i [1] "complex"
Example 6: Working with Raw Data
In R, working with raw data involves dealing with binary data in its raw form, often used for low-level manipulation, such as reading/writing binary files or encoding/decoding data.
For example,
- We start by creating a raw vector using the
as.raw()function, passing hexadecimal values as arguments. - We print the raw vector to see its contents.
- We use the
class()function to check the data type of the raw vector.
R Program
raw_data <- as.raw(c(0x41, 0x42, 0x43))
print(raw_data)
class(raw_data)Output
[1] 41 42 43 [1] "raw"