R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Vectors are one of the fundamental data structures in R. They are ordered collections of elements, all of the same type. In this tutorial, we'll explore how to work with vectors in R.
Vectors are created using the c()
function, which stands for 'concatenate' or 'combine'.
# Numeric vector numeric_vector <- c(1, 2, 3, 4, 5) # Character vector char_vector <- c("A", "B", "C") # Logical vector logical_vector <- c(TRUE, FALSE, TRUE)
You can use variables when creating vectors.
x <- 5 y <- 10 vector <- c(x, y) # This will create a vector with values 5 and 10
You can access vector elements using their indices.
In R, indexing starts at 1, not 0 as in many other programming languages.
numeric_vector <- c(10, 20, 30, 40) print(numeric_vector[1]) # Outputs: 10
Using negative indices will exclude the corresponding element.
print(numeric_vector[-1]) # Outputs all elements except the first one
You can access multiple elements by providing multiple indices.
print(numeric_vector[c(1,3)]) # Outputs: 10 30
You can perform element-wise arithmetic on vectors.
a <- c(1, 2, 3) b <- c(4, 5, 6) sum_vector <- a + b # Outputs: 5 7 9
Logical operations are also element-wise.
c <- c(TRUE, FALSE, TRUE) d <- c(FALSE, FALSE, TRUE) result <- c & d # Outputs: FALSE FALSE TRUE
R has a plethora of built-in functions that can be applied to vectors.
length_vector <- c(10, 20, 30, 40, 50) print(length(length_vector)) # Outputs: 5
print(sum(length_vector)) # Outputs: 150 print(mean(length_vector)) # Outputs: 30
unsorted_vector <- c(20, 5, 15, 10) sorted_vector <- sort(unsorted_vector) print(sorted_vector) # Outputs: 5 10 15 20
You can generate sequences using the :
operator or the seq()
function.
# Using : operator seq1 <- 1:5 # Outputs: 1 2 3 4 5 # Using seq function seq2 <- seq(from = 1, to = 10, by = 2) # Outputs: 1 3 5 7 9
Vectors are foundational in R and provide a versatile tool for data manipulation and statistical analysis. With a good understanding of vectors, you're well on your way to becoming proficient in R. Remember, to get the most out of R, it's essential to have a solid grasp of vector operations and the built-in functions that R provides for them.
Introduction to vectors in R:
# Example of vector creation numeric_vector <- c(1, 2, 3, 4, 5)
Creating vectors in R:
c()
function to create vectors by combining elements.# Example of creating vectors numeric_vector <- c(1, 2, 3, 4, 5) character_vector <- c("apple", "banana", "orange")
R vector operations:
# Example of vector operations x <- c(1, 2, 3) y <- c(4, 5, 6) sum_result <- x + y
Subsetting vectors in R:
# Example of subsetting vectors numeric_vector <- c(1, 2, 3, 4, 5) subset_result <- numeric_vector[2:4]
Vector functions in R:
length()
, mean()
, and others to perform operations on vectors.# Example of vector functions numeric_vector <- c(1, 2, 3, 4, 5) vector_length <- length(numeric_vector) mean_value <- mean(numeric_vector)
R vectorization:
# Example of vectorization x <- c(1, 2, 3) squared_vector <- x^2
Concatenating vectors in R:
c()
or append()
.# Example of concatenating vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) concatenated_vector <- c(vector1, vector2)
Vector indexing in R:
# Example of vector indexing numeric_vector <- c(10, 20, 30, 40, 50) numeric_vector[3] <- 35
Numeric vectors in R:
# Example of numeric vectors numeric_vector <- c(1.5, 2.5, 3.5)
Character vectors in R:
# Example of character vectors character_vector <- c("apple", "banana", "orange")
Logical vectors in R:
# Example of logical vectors logical_vector <- c(TRUE, FALSE, TRUE)
Vector arithmetic in R:
# Example of vector arithmetic x <- c(1, 2, 3) y <- c(4, 5, 6) sum_result <- x + y
Vector manipulation in R:
# Example of vector manipulation numeric_vector <- c(1, 2, 3, 4, 5) squared_vector <- numeric_vector^2
R vector examples:
# Example of vector usage ages <- c(25, 30, 35, 40, 45) average_age <- mean(ages)