# Assign the vector of glengths and species to combined
combined <- c(glengths, species)R Syntax and Data Structures Answer Key
Exercise 1
- Try to create a vector of numeric and character values by combining the two vectors that we just created (
glengthsandspecies). Assign this combined vector to a new variable calledcombined. Hint: you will need to use the combinec()function to do this.
- Print the
combinedvector in the console, what looks different compared to the original vectors?
# Print the combined vector to the console
combined[1] "4.6" "3000" "50000" "ecoli" "human" "corn"
glengths has been coerced into being a character vector.
Exercise 2
Let’s say that in our experimental analyses, we are working with three different sets of cells: normal, cells knocked out for geneA (a very exciting gene), and cells overexpressing geneA. We have three replicates for each celltype.
- Create a vector named
samplegroupwith nine elements: 3 control (“CTL”) values, 3 knock-out (“KO”) values, and 3 over-expressing (“OE”) values.
# Create the samplegroup vector with 3 CTL, 3 KO and 3 OE
samplegroup <- c("CTL", "CTL", "CTL", "KO", "KO", "KO", "OE", "OE", "OE")- Turn
samplegroupinto a factor data structure.
# Convert samplegroup into a factor and re-assign it to samplegroup
samplegroup <- factor(samplegroup)Exercise 3
Create a data frame called favorite_books with the following vectors as columns:
# Create a character vector containing book names and assign it to titles
titles <- c("Catch-22", "Pride and Prejudice", "Nineteen Eighty Four")
# Create a numeric vector containing page numbers and assign it to pages
pages <- c(453, 432, 328)# Create a data frame of titles and pages
favorite_books <- data.frame(titles, pages)Exercise 4
Create a list called list2 containing species, glengths, and number.
# Create a list called list2 composed of species, glengths and number
list2 <- list(species, glengths, number)