Loop Exercise

Exercise 1

Create a dataframe from the “Datasaurus.tsv” file with the following code:

[ ]:
data <- read.csv(
    sep = "\t"

Datasaurus.tsv is a tab-separated-value file, so it’s like a CSV, but it uses tabs not commas to separate columns. We can read this with read.csv, but we have to tell it that the column separator is a tab with the argument sep="\t". Take a look at a few of the top rows:

[ ]:

A data.frame: 6 x 26

Exercise 2

This dataset actually contains 13 separate example datasets, each with two variables named example[number]_x and example[number]_y.

In order to get a better sense of what these datasets look like, our first goal will be to write a loop that iterates over each example dataset (numbered 1 to 13) and prints out the mean and standard deviation for example[number]_x and example[number]_y for each dataset.

For example, the first iteration of this loop might return something like (note I’m also rounding my answers for readability):

Example Dataset 1:
Mean x: 23.123,
Mean y: 98.242,
Std Dev x: 21.247,
Std Dev y: 32.243,
Correlation: -0.742

(Though you shouldn’t get those specific values)

But as we discussed in our reading, we want to approach this in stages. So first, write out the skeleton of your loop. Don’t put anything in it except to print out the value of the variable that is iterating in the loop for now.

Note: the function in R for the correlation between two variables may not be what you think it is…

Exercise 3

Now sketch out what the inside of your loop will look like. As discussed in the readings, begin by creating a variable that takes on the first value you’ll loop over, then, using that variable, write code that does what you want your first pass of the loop to do.

You’ll probably need to make use of print(), paste0(), and round().

Round your answers to three decimal places. Note these numbers should look very similar across datasets.

Exercise 4

Now put them together! Can you print out these summary statistics for all 13 pairs of variables?

Exercise 5

Based only on these results, discuss what might you conclude about these example datasets with your partner. Write down your thoughts.

Exercise 6

Now we’re going to write a loop that plots these datasets out in a scatter plot. To do this, we’ll be using the library ggplot2, a libraries from the tidyverse the is unequivocally the best tool for plotting in R.

We aren’t going to do a full lesson on ggplot because you’re sure to encounter it in the future, but the basic syntax to plot the first dataset would look like this:

[ ]:

# First is the dataset, then within `aes_string()` you specify
# which column from the data is the x-axis and which column is the y-axis,
# then `+ geom_point()` tells ggplot to plot points for each line of the data.

ggplot(data, aes(x = .data[["example1_x"]], y = .data[["example1_y"]])) +


Now build a loop that plots a scatter plot of all 13 of these data pairs. As before, approach it first by writing a skeleton loop that specifies how the loop will work, then write the inside separately, then integrate the two.

Note: if you’ve used ggplot before, note that we’re passing the name of our variables ("example1_x") as a string inside .data[[]]—this avoids the tidyverse odd evaluation problem so you can pass characters as text in double quotes and variables as text not in quotes like normal!

Note: your ggplot may only show up if you wrap it in a print() when it’s inside your loop!

Exercise 7

Review you plots. How does your impression of how these datasets differ from what you wrote down in Exercise 3?


Here’s the deal with programming: the only way to learn to program is to wrestle with solving your own problems. The best way to learn is to do so actively – if you just look at answers as soon as you get stuck, your process is more passive. You may feel like you’re learning more, but research shows that that’s an illusion – students who learn passively think they’re learning more than they are (a summary of work in this area is here).

Moreover, in coding in particular, the process of debugging your code is a critical skill in and of itself, and the only way you will learn to do it is by literally spending hours working through your own problems. And once you’ve seen an answer, there’s no way to unsee it, so proceed only if you absolutely have to.

OK, if you still want to proceed, you can find solutions here.