Question 1

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:

  1. Create an artificial DNA sequence that is 15 bases in length.
sequence = ""
for (i in 1:15) {
  sequence = paste0(sequence, sample(bases, 1))
}
sequence
  1. Create an artificial RNA sequence where thymine "T" is substituted for uracil "U".
bases = c("C", "G", "A", "U")
sequence = ""
for (i in 1:9) {
  sequence = paste0(sequence, sample(bases, 1))
}
sequence
  1. Rewrite the loop in (2) using only the 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 = "")

Question 2

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
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

Solutions are contained within this package:

vignette("solutions1", package = "jrProgBio")


jr-packages/jrProgBio documentation built on Jan. 18, 2020, 1:32 a.m.