Explore tens of thousands of sets crafted by our community.
R Programming Vector Operations
31
Flashcards
0/31
Concatenating vectors using c()
Joins two or more vectors end-to-end. Example: c(vec1, vec2)
Logical OR over vector elements
Performs element-wise logical OR. Example: vec1 | vec2
Applying a function to each element with sapply()
Applies a function to each element in a vector and simplifies the result if possible. Example: sapply(vec, sqrt)
Vector modulo
Calculates the modulus of two vectors element-wise. Example: vec1 %% vec2
Sum of vector elements with sum()
Calculates the sum of all elements in a vector. Example: sum(vec)
Vector subtraction
Subtracts two vectors element-wise. Example: vec1 - vec2
Appending elements to a vector with append()
Adds elements to a vector at a specified position. Example: append(vec, values=c(4, 5), after=2)
Vector multiplication
Multiplies two vectors element-wise. Example: vec1 * vec2
Vector exponentiation
Raises elements of the first vector to the powers of the second vector element-wise. Example: vec1 ^ vec2
Using seq() to generate sequence
Generates a sequence with specific properties. Example: seq(from=1, to=10, by=0.5)
Vector recycling
When applying an operation to two vectors of unequal length, the shorter one is recycled to match the length of the longer. Example: c(1, 2) + c(1, 2, 3, 4)
Minimum value of a vector with min()
Finds the lowest value in a vector. Example: min(vec)
Ordering indices of vector elements with order()
Gives the indices that would sort the vector. Example: order(vec, decreasing=TRUE)
Ranking vector elements with rank()
Assigns ranks to elements in a vector. Example: rank(vec)
Vector sequence with ':' operator
Creates a sequence of numbers. Example: 1:10 creates a vector from 1 to 10
Sampling elements from a vector with sample()
Randomly selects elements from a vector. Example: sample(vec, size=3)
Mean of vector elements with mean()
Calculates the average of all elements in a vector. Example: mean(vec)
Logical comparison between vectors
Compares two vectors element-wise and returns a logical vector. Example: vec1 > vec2
Product of vector elements with prod()
Calculates the product of all elements in a vector. Example: prod(vec)
Median of vector elements with median()
Finds the median value of all elements in a vector. Example: median(vec)
Vector negation
Switches the sign of each vector element. Example: -vec
Vector creation using c() function
Combines values to create a vector. Example: vec <- c(1, 2, 3)
Vector addition
Adds two vectors element-wise. Example: vec1 + vec2
Logical AND over vector elements
Performs element-wise logical AND. Example: vec1 & vec2
Accessing vector elements with []
Retrieves elements at specified indices. Example: vec[c(1, 3)]
Sorting a vector with sort()
Arranges elements in ascending or descending order. Example: sort(vec, decreasing=TRUE)
Using all() to check if all elements are TRUE
Checks if all elements of a logical vector are TRUE. Example: all(vec > 0)
Vector division
Divides two vectors element-wise. Example: vec1 / vec2
Maximum value of a vector with max()
Finds the highest value in a vector. Example: max(vec)
Generating repeating sequences with rep()
Repeats the values in a vector. Example: rep(c(1,2), times=3)
Using any() to check if any element is TRUE
Checks if any element of a logical vector is TRUE. Example: any(vec > 0)
© Hypatia.Tech. 2024 All rights reserved.