In this question we are going to create some simple artificial DNA sequences. We will create a vector of possible bases, then we will write a loop which samples those bases, adding a new base to the end of the sequence at each iteration of the loop.
bases = c("C", "G", "A", "T") sequence = "" for (i in 1:9) { sequence = paste0(sequence, sample(bases, 1)) } sequence
The for
loop above produces the artificial DNA sequence something like this: r sequence
. Make the following changes to the above code:
sequence = "" for (i in 1:15) { sequence = paste0(sequence, sample(bases, 1)) } sequence
bases = c("C", "G", "A", "U") sequence = "" for (i in 1:9) { sequence = paste0(sequence, sample(bases, 1)) } sequence
sample()
function itself. Hint: you can see how sample works by checking the helpfile ?sample
and you can use paste(collapse = "")
to concatenate them.paste(sample(bases, 15, replace = TRUE), collapse = "")
In the notes, we observed that it was straight forward to loop through a data set and select the maximum values:
dd = data.frame(x = rnorm(10), y = rnorm(10), z = rnorm(10)) max_cols = numeric(ncol(dd)) for (i in seq_along(dd)) { max_cols[i] = max(dd[, i]) } max_cols
mean()
instead of the maximum valuesd()
) as well as the mean.
You should only have a single loop!means = numeric(ncol(dd)) sds = numeric(ncol(dd)) for (i in seq_along(dd)) { means[i] = mean(dd[, i]) sds[i] = sd(dd[, i]) }
Solutions are contained within this package:
vignette("solutions1", package = "jrProgBio")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.