Next →

Output:


      

Plots:

Vectors in R

Vectors are one of the most fundamental data structures in R. They are sequences of elements of the same type (numeric, character, or logical).

Creating Vectors

Use the c() function to create a vector:

numbers <- c(2, 4, 6, 8)
names <- c("Alice", "Bob", "Charlie")
flags <- c(TRUE, FALSE, TRUE)

Vectorized Operations

R allows operations to be applied to all elements of a vector at once:

x <- c(1, 2, 3, 4)
y <- c(5, 6, 7, 8)
x + y   # Adds corresponding elements
x * 2   # Multiplies each element by 2

Vectorized operations make R extremely efficient for handling data.