R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
In R, vectors are the most basic data structures and play a central role. They are one-dimensional arrays that can hold numeric data, character data, or logical data. The data in vectors are known as elements.
In this tutorial, we'll discuss the different types of vectors available in R:
There are four primary types of atomic vectors, depending on the type of data they hold.
Used for storing numeric values.
numeric_vector <- c(1.5, 2.5, 3.5, 4.5) print(numeric_vector)
Used for storing integer values. To ensure the vector is of integer type, you can use the L
suffix.
integer_vector <- c(1L, 2L, 3L, 4L) print(integer_vector)
Used for storing boolean values: TRUE
, FALSE
, and NA
(for missing data).
logical_vector <- c(TRUE, FALSE, TRUE, TRUE) print(logical_vector)
Used for storing strings.
character_vector <- c("apple", "banana", "cherry") print(character_vector)
These are used for storing complex numbers.
complex_vector <- c(1+2i, 2+3i) print(complex_vector)
While atomic vectors are homogeneous (all elements are of the same type), lists are heterogeneous. A list can have elements of different types, even other lists.
my_list <- list(1:3, "apple", c(TRUE, FALSE), list("a", "b")) print(my_list)
Factors are used to store categorical data. They can be thought of as integer vectors where each integer has a label.
gender <- c("male", "female", "female", "male", "male") factor_gender <- factor(gender) print(factor_gender)
These are used to store raw bytes.
raw_vector <- charToRaw("Hello, R!") print(raw_vector)
R offers a wide variety of operations that can be performed on vectors:
v1 <- c(1, 2, 3) v2 <- c(4, 5, 6) print(v1 + v2) # Adds the vectors element-wise
v1 <- c(TRUE, FALSE, TRUE) v2 <- c(FALSE, FALSE, TRUE) print(v1 & v2) # Element-wise logical AND
paste()
for string concatenation.str1 <- c("Hello", "R") print(paste(str1, collapse = " "))
Vectors are foundational in R, forming the basis for more advanced data structures like data frames and matrices. Understanding how to create, manipulate, and use the different types of vectors is essential for anyone working with R.
Atomic Vectors in R:
# Example: Atomic vector numeric_vector <- c(1, 2, 3, 4, 5)
Numeric Vectors in R:
# Example: Numeric vector numeric_vector <- c(1, 2, 3, 4, 5)
Character Vectors in R:
# Example: Character vector character_vector <- c("apple", "orange", "banana")
Logical Vectors in R:
# Example: Logical vector logical_vector <- c(TRUE, FALSE, TRUE)
Factor Vectors in R:
# Example: Factor vector factor_vector <- factor(c("low", "medium", "high"), levels = c("low", "medium", "high"))
Complex Vectors in R:
# Example: Complex vector complex_vector <- c(1 + 2i, 3 + 4i, 5 + 6i)
List Vectors in R:
# Example: List vector list_vector <- list(1, "apple", TRUE)
Vector Coercion in R:
# Example: Vector coercion combined_vector <- c(1, "apple", TRUE)
Creating Vectors of Different Types in R:
# Example: Mixed type vector mixed_vector <- c(1, "apple", TRUE)
Operations on Numeric Vectors in R:
# Example: Arithmetic operations on numeric vector result_vector <- numeric_vector * 2
Handling Missing Values in Vectors in R:
NA
to represent missing values in vectors.# Example: Handling missing values in vectors missing_vector <- c(1, NA, 3, 4, 5)
Vector Indexing in R:
# Example: Vector indexing third_element <- numeric_vector[3]
Concatenating Vectors in R:
c()
.# Example: Concatenating vectors combined_vector <- c(numeric_vector, character_vector)
R Vector Types Comparison:
is.numeric()
.# Example: Vector types comparison is_numeric <- is.numeric(numeric_vector)