Introduction to Vectors with R

Introduction to Vectors

Vectors are one of the most fundamental data structures in R. They are used to store collections of elements of the same type, making them versatile and integral to data manipulation and analysis.

Definition and Characteristics

  • Definition: A vector is a one-dimensional array that can hold multiple values of the same type. In R, vectors are used to store and operate on sequences of data.
  • Characteristics:
    • Homogeneity: All elements in a vector must be of the same type (numeric, logical, character, etc.).
    • Indexing: Elements in a vector are accessed using indices. In R, indices start at 1.
    • Length: The length of a vector refers to the number of elements it contains.

Types of Vectors

Vectors in R come in various types depending on the data they store:

  • Numeric Vectors: These store numeric data (both integers and real numbers).
num_vector <- c(1, 2, 3, 4, 5)        # Numeric vector with integers
num_vector <- c(1.1, 2.2, 3.3)        # Numeric vector with real numbers
  • Logical Vectors: These store boolean values (TRUE or FALSE).
log_vector <- c(TRUE, FALSE, TRUE)    # Logical vector
  • Character Vectors: These store strings or characters.
char_vector <- c("apple", "banana", "cherry")   # Character vector
  • Complex Vectors: These store complex numbers (with real and imaginary parts).
complex_vector <- c(1+2i, 3+4i)      # Complex vector

Creating Vectors

Vectors can be created in several ways:

  • Using c() Function: Combine individual elements into a vector.
# Numeric vector
num_vector <- c(10, 20, 30, 40)
# Character vector
char_vector <- c("red", "blue", "green")
# Logical vector
log_vector <- c(TRUE, FALSE, TRUE)
  • Using seq() Function: Generate a sequence of numbers.
seq_vector <- seq(1, 10, by=2)       # Creates a vector: 1, 3, 5, 7, 9
  • Using rep() Function: Repeat elements.
rep_vector <- rep(1:3, times=3)      # Creates a vector: 1, 2, 3, 1, 2, 3, 1, 2, 3

Vector Operations

Vectors in R support a wide range of operations:

  • Arithmetic Operations: Arithmetic operations are applied element-wise.
v <- c(1, 2, 3, 4)
v + 10         # Adds 10 to each element: 11, 12, 13, 14
v * 2          # Multiplies each element by 2: 2, 4, 6, 8
  • Statistical Functions: Functions like mean(), sum(), median(), etc., can be applied to vectors.
mean(v)        # Calculates the mean: 2.5
sum(v)         # Calculates the sum: 10
median(v)      # Calculates the median: 2.5
  • Logical Operations: Logical operations are also element-wise. 
v > 2          # Returns a logical vector: FALSE, FALSE, TRUE, TRUE

Indexing and Slicing

Vectors are indexed starting from 1. You can access individual elements, subsets, or modify elements using indices.

  • Accessing Elements: Use indices to get specific elements.
v[3]           # Returns the 3rd element: 3
  • Slicing Vectors: Use a range or a vector of indices to access multiple elements.
v[2:4]         # Returns elements from index 2 to 4: 2, 3, 4
v[c(1, 4)]     # Returns elements at indices 1 and 4: 1, 4
  • Negative Indices: Use negative indices to exclude elements. 
v[-2]          # Excludes the 2nd element: 1, 3, 4

 Vector Properties

  • Length: Get the number of elements using length().
length(v)      # Returns 4
  • Class: Check the type of the vector using class().
class(v)       # Returns "numeric"
  • Names: Assign names to elements of a vector for easier identification.
named_vector <- c(a=1, b=2, c=3)
named_vector["b"]  # Returns 2

Example Usage

Here are a few practical examples of using vectors:

  • Creating a Numeric Sequence:
# Create a vector of the first 10 natural numbers
numbers <- 1:10
  • Filtering Data:
# Create a vector of ages
ages <- c(21, 25, 30, 35, 40)
# Filter ages greater than 30
ages_over_30 <- ages[ages > 30]  # Returns 35, 40
  • Combining Vectors
# Combine two vectors
vector1 <- c(1, 2, 3)
vector2 <- c(4, 5, 6)
combined_vector <- c(vector1, vector2)  # Returns 1, 2, 3, 4, 5,

In summary, vectors in R are essential for data manipulation and analysis. They are simple, one-dimensional arrays that hold elements of the same type and support various operations and functions. Understanding vectors’ properties and how to manipulate them is crucial for effective data handling in R.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *