1/52
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
echo=FALSE, eval=FALSE
Need to fill in the code chunk
Type of information stored in vectors
A sequence of elements of the same type
Common storage uses of vectors in bioinformatics
Gene expression values, sample identifiers, statistical measurements, experimental conditions
Key properties of vectors
All elements must be of the same type, R uses 1-based indexing (first element is at position 1), vectors can be numeric/character/logical
c()
Combines individual values
numbers <- c(1, 2, 3, 4, 5)
Creates a numeric vector called ‘numbers’ - 1, 2, 3, 4, 5
sequence <- 1:10
Creates a numeric vector called ‘sequence’ - numbers 1 to 10
repeated <- rep(5, 3)
Creates a numeric vector called ‘repeated’ - 5, 5, 5
seq()
Creates sequences with specific patterns
rep()
Repeat values
:
Simple integer sequences
<- c(2.5, 3.1, 1.8, 4.2, 2.9)
Creates a vector containing specific values - 2.5, 3.1, 1.8, 4.2, and 2.9
<- 1:10
Creates a vector containing the numbers from 1 to 10
<- seq(2, 20, by = 2)
Creates a vector containing a sequence of even numbers from 2 to 20
<- seq(0,24, length.out = 5)
Creates a vector containing a sequence with a specific length, 5 points from 0 to 24 - 0, 6, 12, 18, 24
<- rep(1, 5)
Creates a vector of 1 repeated 5 times
<- rep(c(“A”, “B”), each = 3)
Creates a vector with a repeated sequence, three A’s followed by three B’s
name <- c(“I”, “Am”, “Flo”)
Creates a character vector called ‘name’ - I, Am, Flo
samples <- paste("Sample", 1:5, sep = "_")
Creates a character vector called ‘samples’ - Sample_1, Sample_2, Sample_3, Sample_4, Sample_5
paste(x, 1:3, sep = /)
Paste joins strings together. Because R is vectorized, it takes the first element ‘x’ and attaches it to each number in the sequence. Then separates the x from the number with ‘/’ - x/1, x/2, x/3
length()
Displays number of elements in the vector in parentheses
TRUE/FALSE
Used for logical values
“ “
Used for character values
<- c(TRUE, FALSE, TRUE)
Creates a logical vector for conditions - Yes, no, yes
<- vector > 3.0
Creates a logical vector from comparison - new vector contains only the values in ‘vector’ higher than 3.0
<- vector * 2
Vector operation that doubles each element in the vector, ‘vector’ by 2
<- vector + numbers
Vector operation that adds the two vectors, ‘vector’ and' ‘numbers’ element-wise
<- log(vector)
Vector operation that takes the natural logarithm of each element in the vector, ‘vector’
<- vector > 2.0 & vector < 4.0
Combines logical conditions to creat a vector of all the values between 2.0 and 4.0 in the vector, ‘vector’
sum(vector)
Displays sum of all values of ‘vector’
mean(vector)
Displays average value of ‘vector’
median(vector)
Displays median value of ‘vector’
sd(vector)
Displays standard deviation of ‘vector’
numbers[1]
Accesses the first element of the vector, ‘numbers’
numbers[1:3]
Accesses the first three elements of the vector, ‘numbers’
numbers[c(1, 3, 5)]
Accesses specific elements of the vector, ‘numbers’ - elements 1, 3, and 5
numbers[length(numbers)]
Accesses the last element of the vector, ‘numbers’
numbers[numbers > 3]
Accesses elements greater than 3 in the vector, ‘numbers’
numbers[-1]
Accesses all elements except the first in the vector, ‘numbers’
numbers[-(4:5)]
Accesses all elements except the fourth and fifth in the vector, ‘numbers’
modified_vector[1] <- 5.0
Changes the first element in the vector, ‘modified_vector’ to 5.0
modified_vector[c(2, 4)] <- c(1.0, 1.5)
Changes the 2nd and 4th elements in the vector, ‘modified_vector’ to 1.0 and 1.5
numbers[numbers %% 2 == 0]
Logically indexes even numbers in the vector, ‘numbers’
which.max(vector)
Finds the highest value in the vector, ‘vector’
sample_IDs[which.max(numbers)]
Finds the position of the highest number and gets the corresponding sample name
sort()
Arranges values in ascending order
sort(x, decreasing = TRUE)
Arranges values in descending order
unique()
Removes duplicate values
which()
Finds positions of values that meet the logical condition in the parentheses
rev()
Reverses the order of values
c(vector, numbers[1:3])
Combines ‘vector’ with ‘number’ elements 1 to 3
<- vector[vector > 3.0]
Subsets only values over 3.0 from ‘vector’ into a new variable
prod(vector)
Calculates the product of all values in ‘vector’