Intro to Coding
Exercises
Note: more practice exercise are available, for this and other lessons, under “extras” column on the course homepage calendar rows.
Exercises: (don’t forget to read the note above ⬆)
Exercise 1: Terrestrial Planets
Consider the following data set—shown in the table below—containing variables of so-called Terrestrial planets. These planets include Mercury, Venus, Earth, and Mars. They are called like this because they are “Earth-like” planets: relatively small in size and in mass, with a solid rocky surface, and metals deep in its interior.
| name | gravity | moons |
|---|---|---|
| Mercury | 3.7 | 0 |
| Venus | 8.9 | 0 |
| Earth | 9.8 | 1 |
| Mars | 3.7 | 2 |
- Consider the column name in the provided table of terrestrial planets. Use the
c()function to create a character vectornamecontaining the names of the Terrestrial planets.
Solution:
name = c("Mercury", "Venus", "Earth", "Mars")
- Consider the column gravity in the provided table of terrestrial planets. Use the combine function
c()to make a numeric vectorgravityfor the Terrestrial planets.
Solution:
gravity = c(3.7, 8.9, 9.8, 3.7)
- Consider the column moons in the provided table of terrestrial planets. Use the combine function
c()to makemoons.
Solution:
moons = c(0, 0, 1, 2)Exercise 2: Make a data frame from scratch
Use the vectors that you defined in the previous section in order to create a data frame planets:
Solution:
planets <- data.frame(
name, gravity, moons)Also notice: this is one command, spread over two lines. You’ve seen other examples like this but we haven’t said anything about it yet.
When writing code, for readability, you can almost always spread your commands across multiple lines like this. As long as the first line is an “incomplete” command, it will keep reading more lines until it gets a full command. Since planets <- data.frame( kind of leaves R hanging, waiting for more, it will keep going and bring in the next line too, which makes a complete command.
So all of the following are equivalent to R (but some are easier)
planets <- data.frame(name, gravity, moons)planets <- data.frame(
name, gravity, moons)planets <- data.frame(
name,
gravity,
moons
)planets <- data.frame(
name, gravity,
moons
)The “whitespace” (spaces, new lines, etc) is ignored until it finds a full command. All of these work.
So arrange your code readably, please.
Exercise 3: all by yourself
Let’s apply everything that you’ve learned so far in order to create a data frame students containing the following data, and the provided specifications listed below:
| name | height | year | resident |
|---|---|---|---|
| Leia | 160 | sophomore | TRUE |
| Luke | 170 | freshman | FALSE |
| Han | 182 | senior | TRUE |
| Lando | 178 | junior | FALSE |
name: nominal variable (character)heightcontinuous variable (numeric)year: ordinal categorical variable (usefactor…)resident: logical variable (use TRUE and FALSE, without quotes, as values)
Name the final dataframe students.
Solution:
students = data.frame(
"name" = c("Leia", "Luke", "Han", "Lando"),
"height" = c(160, 170, 182, 178),
"year" = factor(x = c("sophomore", "freshman", "senior", "junior"),
levels = c("freshman", "sophomore", "junior", "senior")),
"resident" = c(TRUE, FALSE, TRUE, FALSE)
)Want more practice? Studying for a quiz?
Try the “extras” for this week - on the homepage you’ll see a link to “Coding Basics” with more simple coding problems. Or click here to go there directly.