R Programming language - R Vectors

https:/‮figi.www/‬tidea.com

In R programming language, a vector is a one-dimensional array of values of the same data type. Vectors are one of the most basic and commonly used data structures in R. Here are some examples of vectors in R:

num_vec <- c(1, 2, 3, 4, 5)
char_vec <- c("apple", "banana", "orange")
bool_vec <- c(TRUE, FALSE, TRUE, FALSE)

In R, you can create a vector using the c() function, which concatenates values into a vector. You can also create a vector using the : operator, which creates a sequence of numbers. Here are some examples:

num_vec <- c(1, 2, 3, 4, 5)
num_seq <- 1:5 # Creates a vector containing the numbers 1 through 5
char_vec <- c("apple", "banana", "orange")
bool_vec <- c(TRUE, FALSE, TRUE, FALSE)

You can access individual elements of a vector using square bracket notation, like this:

num_vec <- c(1, 2, 3, 4, 5)
print(num_vec[3]) # Output: 3

You can also access multiple elements of a vector using a vector of indices in square brackets, like this:

num_vec <- c(1, 2, 3, 4, 5)
print(num_vec[c(2, 4)]) # Output: 2 4

You can perform operations on vectors using vectorized functions, which operate on all elements of a vector at once. Here are some examples:

num_vec1 <- c(1, 2, 3, 4, 5)
num_vec2 <- c(6, 7, 8, 9, 10)
sum_vec <- num_vec1 + num_vec2 # Adds corresponding elements of the two vectors
prod_vec <- num_vec1 * num_vec2 # Multiplies corresponding elements of the two vectors

Understanding how to work with vectors in R is important for many data manipulation and analysis tasks, as vectors are used to store and operate on large sets of data.