Vector Declarations in R

Vector Declarations in R

Vectors are fundamental data structures in R that store elements of the same type. Here’s an in-depth look at how to declare and create vectors in R.

Creating Numeric Vectors

Numeric vectors contain numbers.

Example: 

# Creating an integer numeric vector
num_vec_int <- c(1, 2, 3, 4, 5)
# Creating a floating-point numeric vector
num_vec_float <- c(1.1, 2.2, 3.3, 4.4, 5.5)

 Notes:

  • Numeric vectors can contain either integers (1, 2, 3) or floating-point numbers (1.1, 2.2).
  • R automatically converts integers to floating-point numbers if necessary to maintain type consistency.

Creating Character Vectors

Character vectors store text strings.

Example: 

# Creating a character vector
char_vec <- c("apple", "banana", "cherry")
# Mixing text strings with numbers
char_mixed <- c("Age: ", 25)

Notes:

  • If a vector contains characters and numbers, R converts all elements to characters to maintain type consistency.

Creating Logical Vectors

Logical vectors store boolean values: TRUE or FALSE.

Example: 

# Creating a logical vector
log_vec <- c(TRUE, FALSE, TRUE, FALSE)

Notes:

  • Logical vectors are often used for conditions and filtering.

Creating Complex Vectors

Complex vectors can store complex numbers (which have a real and imaginary part).

Example: 

# Creating a complex vector
comp_vec <- c(1+2i, 3+4i, 5+6i)

Notes:

  • Complex numbers are noted with i for the imaginary part (e.g., 3+4i).

Creating Vectors with Element Names

Element names can be assigned to make vectors more readable.

Example: 

# Creating a named vector
named_vec <- c(a = 1, b = 2, c = 3)
# Accessing an element by name
element_b <- named_vec["b"]

Notes:

  • Element names allow you to reference vector values by names rather than numeric indices.

Creating Vectors with the vector() Function

The vector() function allows you to create vectors of specific types with an initial length.

Example: 

# Creating a numeric vector of length 5
vec_numeric <- vector("numeric", length = 5)
# Creating a logical vector of length 3
vec_logical <- vector("logical", length = 3)

Notes:

  • vector(“numeric”, length = n) creates a numeric vector of length n with initial values set to 0.
  • vector(“logical”, length = n) creates a logical vector of length n with initial values set to FALSE.

Using the c() Function to Combine Vectors

The c() function can also be used to concatenate multiple vectors into a single vector.

Example: 

# Creating two vectors
vec1 <- c(1, 2, 3)
vec2 <- c(4, 5, 6)
# Combining the two vectors
combined_vec <- c(vec1, vec2)

Notes:

  • The c() function concatenates multiple vectors into a single vector.

Using the seq() Function to Create Sequential Vectors

The seq() function generates sequences of numbers.

Example: 

# Creating a sequence from 1 to 10 with a step of 1
seq_vec <- seq(1, 10, by = 1)
# Creating a sequence with a specific length
seq_len_vec <- seq_len(5)  # Equivalent to seq(1, 5)

 Notes:

  • seq(from, to, by) allows you to specify the start, end, and step of the sequence.
  • seq_len(n) generates a sequence from 1 to n.

Using the rep() Function to Repeat Elements

The rep() function allows you to repeat elements of a vector.

Example: 

# Repeating each element of c(1, 2) twice
repeated_vec <- rep(c(1, 2), each = 2)
# Repeating the vector c(1, 2) three times
repeated_vec_times <- rep(c(1, 2), times = 3)

Notes:

  • rep(x, each = n) repeats each element n times.
  • rep(x, times = n) repeats the entire vector n times.

Creating Zero-Length Vectors

Vectors can also be created with zero length, which can be useful for subsequent operations.

Example: 

# Creating an empty vector
empty_vec <- c()
# Checking the length of the vector
length(empty_vec)  # Result: 0

Notes:

  • Empty vectors are often used as a base to conditionally add elements later.

Laisser un commentaire

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