All Study Guides Intro to Programming in R Unit 3
💻 Intro to Programming in R Unit 3 – Vectors: Basics and Operations in RVectors are the fundamental building blocks of data in R, serving as one-dimensional arrays that hold multiple values of the same type. They enable efficient storage and manipulation of data, forming the basis for more complex structures like matrices and data frames.
R offers various methods to create and work with vectors, including basic arithmetic operations, element access, and specialized functions. Understanding vector types, coercion, and common operations is crucial for effective data analysis and manipulation in R programming.
What Are Vectors?
Vectors are one-dimensional arrays that can hold multiple values of the same data type
Serve as the most basic data structure in R and are widely used for storing and manipulating data
Can contain numeric (integer or double), character, logical, or complex values
Have a length attribute that represents the number of elements in the vector
Enable efficient storage and processing of data by allowing operations to be performed on multiple values simultaneously
Provide a foundation for more complex data structures in R, such as matrices and data frames
Play a crucial role in data analysis and manipulation tasks, such as subsetting, filtering, and transforming data
Creating Vectors in R
Use the c()
function to create vectors by combining multiple values separated by commas
Example: my_vector <- c(1, 2, 3, 4, 5)
Specify the data type of the vector elements using the appropriate function: numeric()
, character()
, logical()
, or complex()
Create vectors with repeated values using the rep()
function
Generate sequences of numbers using the :
operator or the seq()
function
Example: sequence_vector <- 1:10
Create vectors with specific patterns or sequences using functions like seq()
and rep()
Combine existing vectors using the c()
function to create a new vector
Create named vectors by assigning names to the elements using the names()
function or during vector creation
Vector Types and Coercion
R supports four main types of vectors: numeric, character, logical, and complex
Numeric vectors contain integer or double values and are the most commonly used type
Character vectors store text or string values enclosed in quotes
Logical vectors hold TRUE or FALSE values and are often used for conditional operations
Complex vectors contain complex numbers with real and imaginary parts
Vector elements must be of the same data type within a single vector
R performs automatic type coercion when elements of different types are combined in a vector
Coercion follows a hierarchy: logical < integer < double < character
Use explicit coercion functions like as.numeric()
, as.character()
, or as.logical()
to convert vectors to a specific type
Be cautious when combining vectors of different types to avoid unintended coercion results
Accessing Vector Elements
Access individual elements of a vector using square brackets []
and the element's index
R uses 1-based indexing, meaning the first element has an index of 1
Retrieve multiple elements by providing a vector of indices inside the square brackets
Example: my_vector[c(1, 3, 5)]
Use negative indices to exclude specific elements from the selection
Example: my_vector[-c(2, 4)]
Access elements based on logical conditions using logical vectors or comparison operators
Example: my_vector[my_vector > 3]
Extract elements using named indices if the vector has named elements
Example: my_vector["name"]
Utilize the which()
function to find the indices of elements satisfying a specific condition
Subset vectors using the subset()
function and specify the desired condition
Basic Vector Operations
Perform element-wise arithmetic operations on vectors of the same length
Addition: vector1 + vector2
Subtraction: vector1 - vector2
Multiplication: vector1 * vector2
Division: vector1 / vector2
Carry out arithmetic operations between a vector and a scalar value
Compare vectors element-wise using comparison operators (>
, <
, >=
, <=
, ==
, !=
)
Returns a logical vector indicating the result of each comparison
Combine vectors element-wise using logical operators (&
, |
, !
)
Find the length of a vector using the length()
function
Calculate summary statistics of a vector, such as sum()
, mean()
, min()
, max()
, and median()
Vector Functions in R
length()
: Returns the number of elements in a vector
sum()
: Calculates the sum of all elements in a numeric vector
mean()
: Computes the arithmetic mean of a numeric vector
min()
and max()
: Find the minimum and maximum values in a vector, respectively
sort()
: Sorts the elements of a vector in ascending or descending order
unique()
: Returns a vector with duplicated elements removed
rev()
: Reverses the order of elements in a vector
paste()
: Concatenates elements of a character vector into a single string
grep()
and grepl()
: Search for patterns within a character vector
is.na()
: Checks for missing values (NA) in a vector
any()
and all()
: Test for the presence of any or all TRUE values in a logical vector
Applying Functions to Vectors
Apply functions to each element of a vector using the sapply()
or lapply()
functions
sapply()
simplifies the output to a vector, matrix, or list
lapply()
always returns a list
Use anonymous functions or pre-defined functions as the first argument to sapply()
or lapply()
Perform conditional operations on vector elements using the ifelse()
function
Applies a different function or value based on a logical condition
Utilize the apply()
function to apply a function over the margins of an array or matrix
Employ vectorized functions that automatically operate on each element of a vector without the need for explicit looping
Examples: sqrt()
, log()
, exp()
, round()
, floor()
, ceiling()
Leverage the Vectorize()
function to create vectorized versions of non-vectorized functions
Common Vector Mistakes and How to Avoid Them
Forgetting to use the c()
function when creating vectors, leading to unexpected results
Make sure to combine elements using c()
to create a proper vector
Mixing data types within a vector, causing automatic coercion and potential loss of information
Ensure that all elements in a vector have the same data type
Accessing vector elements using incorrect indices or forgetting that R uses 1-based indexing
Double-check the indices and remember that the first element has an index of 1
Attempting to perform operations on vectors of different lengths without proper recycling
Ensure that vectors have compatible lengths or use appropriate recycling mechanisms
Ignoring warning messages about coercion or length mismatches, leading to unexpected behavior
Pay attention to warning messages and investigate the cause of any issues
Forgetting to handle missing values (NA) appropriately in vector operations and functions
Use functions like is.na()
and na.omit()
to detect and handle missing values
Incorrectly assuming that operations on vectors are performed in a element-wise manner
Be aware of the behavior of different operators and functions when applied to vectors