Expected Preparations:
|
|||||||
|
|||||||
Keywords: R Lists | |||||||
|
|||||||
Objectives:
This unit will …
|
Outcomes:
After working through this unit you …
|
||||||
|
|||||||
Deliverables: Time management: Before you begin, estimate how long it will take you to complete this unit. Then, record in your course journal: the number of hours you estimated, the number of hours you worked on the unit, and the amount of time that passed between start and completion of this unit. Journal: Document your progress in your Course Journal. Some tasks may ask you to include specific items in your journal. Don’t overlook these. Insights: If you find something particularly noteworthy about this unit, make a note in your insights! page. |
|||||||
|
|||||||
Evaluation: NA: This unit is not evaluated for course marks. |
Introduction to R list data types: properties, how to create, and modify them and how to retrieve data.
Task…
R-Exercise_BasicSetup
project in RStudio if
you don’t already have it open.init()
as instructed after the project has
loaded.
The elements contained in matrices or arrays must all have the same type – character, numeric, logical or such. Data frames allow us to store elements of different types in columns, but all columns must have the same length and the elements have to be “atomic” - i.e. you can’t put vectors into dataframe columns. But R’s lists are much more versatile. They are simply ordered collections of components. These components can have different type - all kinds of R objects can go into a list: characters, booleans, any kind of numeric data, other lists, even functions - AND they can have different size.
Lists are created with the list()
function, which works
similar to the c()
function for vectors. Components are
accessed through their index in double square brackets, or through their
name, using the “$” operator, if the name has been defined. Here is an
example:
pUC19 <- list(size=2686, marker="ampicillin", ori="ColE1", accession="L01397", BanI=c(235, 408, 550, 1647) )
objectInfo(pUC19)
pUC19[[1]]
pUC19[[2]]
pUC19$ori
pUC19$BanI[2]
Note that in our data.frame()
example, we stored
multiple restriction enzymes in one string, separated
by commas. While we can take such strings apart again, by using the
strsplit()
function, the string itself still has to be one
single element in the data frame’s column. Lists have no such
restriction. In our example above, we assigned a vector of restriction
site positions to the element “BanI”.
You can easily imagine that we could now create a list of lists, and that list of lists could hold an entire plasmid database in a most versatile way. Let’s do this!
Task…
plasmidDB <- list()
plasmidDB[["pUC19"]] <- pUC19
Whereas data frames allow you to get all data from a column directly,
this is not possible for lists. You need a function that iterates over
all list elements instead. Such a function is lapply()
, one
of several “apply” functions. For example, to get all “ori” elements,
try:
lapply(plasmidDB, function(x) { return(x$ori) })
unlist()
to
flatten the result. Then use min()
to find the size of the
smallest one.
How do I …
# Initialize an empty list?
#
# You need this if you want to pre-allocate a given number of empty
# slots into which you can place data later. In contrast to vectors, you
# can't just write to a non-existing slot of a list - the slot has to
# exist, although it may certainly be empty:
myList <- vector(mode = "list", length = 3)
myList
# [[1]]
# NULL
#
# [[2]]
# NULL
#
# [[3]]
# NULL
# Store or modify an element in a list?
#
myList[[2]]$xy <- c(1.4142, 1.618)
myList
# [[1]]
# NULL
#
# [[2]]
# [[2]]$xy
# [1] 1.4142 1.6180
#
#
# [[3]]
# NULL
# Iterate over all elements of a list?
#
# Loop over the list length ...
for (i in seq_len(length(myList))) {
print(myList[[i]]$xy)
}
# NULL
# [1] 1.4142 1.6180
# NULL
# Append an element to a list ...
#
# Just use c(), and append an empty list or a list with values.
#
myList <- c(myList, NULL) # This does nothing!
myList <- c(myList, list(NULL)) # This appends a new slot
myList[[length(myList)]]$myNeighbour <- "Totoro" # This puts a named element
# into the last slot
myList
# [[1]]
# NULL
#
# [[2]]
# [[2]]$xy
# [1] 1.4142 1.6180
#
#
# [[3]]
# NULL
#
# [[4]]
# [[4]]$myNeighbour
# [1] "Totoro"
# Remove an elelment from a list?
#
# Just assign NULL to its slot ...
myList[[3]] <- NULL
myList
# [[1]]
# NULL
#
# [[2]]
# [[2]]$xy
# [1] 1.4142 1.6180
#
#
# [[3]]
# [[3]]$myNeighbour
# [1] "Totoro"
Practice these steps from time to time until they feel natural.
Execute: x <- strsplit(plasmidData$Sites, ", ")
and
analyze the result.
plasmidData$Sites
?x
?strsplit()
have a list as return value, not a
vector or a data frame?
If in doubt, ask! If anything about this contents is not clear to you, do not proceed but ask for clarification. If you have ideas about how to make this material better, let’s hear them. We are aiming to compile a list of FAQs for all learning units, and your contributions will count towards your participation marks.
Improve this page! If you have questions or comments, please post them on the Quercus Discussion board with a subject line that includes the name of the unit.
[END]