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.
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.
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)
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)
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)
The logical
data type is used to store boolean values: TRUE
or FALSE
.
flag <- TRUE
print(flag)
class(flag)
The complex
data type is used to store complex numbers.
comp <- 3 + 4i
print(comp)
class(comp)
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)
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,
num
and assign it a floating-point value of 42.5.num
to the console.class
function to determine the data type of num
and print the result.num <- 42.5
print(num)
print(class(num))
[1] 42.5 [1] "numeric"
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,
int
and assign it an integer value of 42 using the L
suffix.int
to the console.class
function to determine the data type of int
and print the result.int <- 42L
print(int)
print(class(int))
[1] 42 [1] "integer"
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,
char
and assign it a string value of "Hello, R!".char
to the console.class
function to determine the data type of char
and print the result.char <- "Hello, R!"
print(char)
print(class(char))
[1] "Hello, R!" [1] "character"
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,
flag
and assign it a value of TRUE
.flag
to the console.class
function to determine the data type of flag
and print the result.flag <- TRUE
print(flag)
print(class(flag))
[1] TRUE [1] "logical"
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,
comp
and assign it a value of 3 + 4i.comp
to the console.class
function to determine the data type of comp
and print the result.comp <- 3 + 4i
print(comp)
print(class(comp))
[1] 3+4i [1] "complex"
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,
as.raw()
function, passing hexadecimal values as arguments.class()
function to check the data type of the raw vector.raw_data <- as.raw(c(0x41, 0x42, 0x43))
print(raw_data)
class(raw_data)
[1] 41 42 43 [1] "raw"