Vectors in R Programming

Introduction

Before reading this article, please learn about the basics of R Programming in my previous article on C#Corner: R Programming 

Vectors are the building blocks of data in R programming. Whether you're a beginner or an experienced data analyst, understanding vectors is crucial to harnessing the power of R for data manipulation and analysis. In this comprehensive guide, we'll delve deep into the world of vectors in R. We'll start by defining what vectors are, exploring the various types of vectors, examining the syntax for creating and manipulating vectors, and providing illustrative examples to solidify your understanding.

What Are Vectors?

In R, a vector is a fundamental data structure that allows you to store and manipulate a sequence of values of the same data type. These values can be numbers, characters, logical values, or other data types. Vectors in R are one-dimensional, meaning they contain elements arranged in a single row or column.

Types of Vectors

R supports several types of vectors based on the data they can hold. Let's take a closer look at each of these types.

1. Numeric Vectors

Numeric vectors, also known as double vectors, are used to store numerical values, including integers and real numbers (numbers with decimal points).

Syntax
c(value1, value2, ...)

Example

# Creating a numeric vector
numeric_vector <- c(1, 2, 3.5, -4, 0)

2. Character Vectors

Character vectors store sequences of characters, such as letters, words, or sentences. Elements in a character vector are enclosed in double or single quotes.

Syntax
c("value1", "value2", ...)

Example

# Creating a character vector
character_vector <- c("apple", "banana", "cherry")

3. Logical Vectors

Logical vectors hold logical values, which can be either TRUE, FALSE, or NA (representing missing or undefined values).

Syntax
c(TRUE, FALSE, ...)

Example

# Creating a logical vector

logical_vector <- c(TRUE, FALSE, TRUE, NA, TRUE)

4. Integer Vectors

Integer vectors are specifically designed to store whole numbers (integers). In contrast to numeric vectors, which store both integers and decimals, integer vectors only store whole numbers.

Syntax
as.integer(c(value1, value2, ...))

Example

# Creating an integer vector

integer_vector <- as.integer(c(1, 2, 3, 4, 5))

5. Factor Vectors

Factor vectors are used to represent categorical data. They are similar to character vectors, but they have predefined levels or categories, which makes them suitable for statistical analysis.

Syntax
factor(c("category1", "category2", ...))

Example

# Creating a factor vector

factor_vector <- factor(c("low", "medium", "high", "medium", "low"))

6. Complex Vectors

Complex vectors are used to store complex numbers, which consist of a real part and an imaginary part. These are often used in advanced mathematical and engineering applications.

Syntax
cplx <- complex(real = real_part, imaginary = imaginary_part)

Example

# Creating a complex vector

complex_vector <- complex(real = c(1, 2, 3), imaginary = c(0, 1, -2))

Now that we've covered the types of vectors in R, let's move on to the syntax for creating and manipulating vectors.

Creating Vectors in R


Vector Creation Syntax

Creating vectors in R is straightforward. You can use the c() function (short for "combine") to concatenate values into a vector. Here's the general syntax:

vector_name <- c(value1, value2, ...)

Vector Creation Examples

Let's create vectors of different types using the syntax described above.

Numeric Vector Example

# Creating a numeric vector

numeric_vector <- c(1, 2, 3.5, -4, 0)

Character Vector Example

# Creating a character vector

character_vector <- c("apple", "banana", "cherry")

Logical Vector Example

# Creating a logical vector

logical_vector <- c(TRUE, FALSE, TRUE, NA, TRUE)

Integer Vector Example

# Creating an integer vector

integer_vector <- as.integer(c(1, 2, 3, 4, 5))

Factor Vector Example

# Creating a factor vector

factor_vector <- factor(c("low", "medium", "high", "medium", "low"))

Complex Vector Example

# Creating a complex vector

complex_vector <- complex(real = c(1, 2, 3), imaginary = c(0, 1, -2))

Vector Operations and Manipulations

Now that you know how to create vectors, it's essential to understand how to manipulate and perform operations on them.

Vector Arithmetic Operations

You can perform various arithmetic operations on numeric vectors, such as addition, subtraction, multiplication, and division. These operations are applied element-wise.

Example

# Numeric vector operations
vector1 <- c(1, 2, 3)
vector2 <- c(4, 5, 6)

# Addition
result_add <- vector1 + vector2  # [5, 7, 9]

# Subtraction
result_sub <- vector1 - vector2  # [-3, -3, -3]

# Multiplication
result_mul <- vector1 * vector2  # [4, 10, 18]

# Division
result_div <- vector1 / vector2  # [0.25, 0.4, 0.5]

Vector Concatenation

You can concatenate vectors using the c() function or the append() function.

Example

# Vector concatenation
vector1 <- c(1, 2, 3)
vector2 <- c(4, 5, 6)

# Using c()
concatenated_vector <- c(vector1, vector2)  # [1, 2, 3, 4, 5, 6]

# Using append()
concatenated_vector <- append(vector1, vector2)  # [1, 2, 3, 4, 5, 6]

Vector Indexing and Subsetting

You can access specific elements within a vector using indexing. In R, indexing starts from 1. You can also use logical vectors to subset data based on conditions.

Example

# Vector indexing and subsetting
numeric_vector <- c(10, 20, 30, 40, 50)

# Accessing the first element
first_element <- numeric_vector[1]  # 10

# Accessing a range of elements
subset_vector <- numeric_vector[2:4]  # [20, 30, 40]

# Subsetting based on a condition
subset_condition <- numeric_vector[numeric_vector > 25]  # [30, 40, 50]

Vector Length and Summary Statistics

You can find the length of a vector using the length() function and calculate summary statistics such as mean, median, and standard deviation.

Example

# Vector length and summary statistics
numeric_vector <- c(10, 20, 30, 40, 50)
print(numeric_vector)

# Length of the vector
vector_length <- length(numeric_vector)  # 5
print(vector_length)

# Mean
mean_value <- mean(numeric_vector)  # 30
print(mean_value)

# Median
median_value <- median(numeric_vector)  # 30
print(median_value)

# Standard deviation
sd_value <- sd(numeric_vector) # 15.81139
print(sd_value)

Output

R Vectors

Conclusion

Vectors are the foundation of data manipulation and analysis in R programming. Understanding the types of vectors, their creation syntax, and how to manipulate them is essential for working with data effectively. Armed with this comprehensive guide, you're now equipped to create, manipulate, and perform operations on vectors, making you better prepared to tackle data analysis tasks using R. Vectors are just the beginning of your journey into the world of R, and as you delve deeper, you'll discover even more powerful data structures and analytical tools at your disposal.

Happy coding!


Similar Articles