Exercises for Chapter 3
Exercise 1: Data Structure and Content
In this exercise, we think about the idea of a data structure more generally. There is no need to write R code!
Tables are only one type of data structure. From your experience with R, do you know others? What are their properties?
Computers store images as sets of pixels. Can you describe what the data structure of a pixelized image looks like? How do we obtain individual values from this data structure? What is the “content” of this data structure?
Imagine we want to propose a simple data structure, a “queue.” This data structure should store information on customers waiting to be served at a business. What properties should the queue have? What minimal operations for adding and removing customers do we need?
Solution
- Vectors: can store sequences of values. Indexes start with 1. Have a particular type (numeric, character). Single columns of data frames (extracted with the
$operator) are vectors. Example:
example <- c(5, 2, 8, 1)
example[1]
## [1] 5
An image is a two-dimensional matrix of pixels. Each pixel can be addressed with its x and y coordinates, each of which is typically a non-negative value. Each pixel can hold a single color value, encoded as a number.
The queue needs to maintain the order of customers: those that arrived later are further behind in the queue. We need at least two basic operations: adding a customer at the end of the queue, and removing the first customer (when served).
Exercise 2: Table Columns and Types in R
As you know, R stores tabular data in “data frames”. In this exercise, we take a closer look at the columns of data frames and their types. Here, you should use R to experiment!
Use the
tdbdata frame with two countries and three columns (country, population, capital) that we created in the chapter. What type does the population variable have? Add a third country (Germany) and the following population value:82 million. What happens to the type of the population variable?In your dataset, somebody mistakenly changes Germany’s name to a numeric country identifier (265). How is this done in R? What happens to the type of the column? Why?
Now, we drop Germany from the data. We also want to fix the type of the population column. How can we do this?
Solution
tdb <- data.frame(
country = c("Switzerland", "Austria"),
population = c(8.3, 8.7),
capital = c("Bern", "Vienna")
)
typeof(tdb$population)
## [1] "double"
tdb <- rbind(tdb, c("Germany", "82 million", "Berlin"))
typeof(tdb$population)
## [1] "character"
tdb[tdb$country == "Germany", "country"] <- 265
typeof(tdb$country)
## [1] "character"
R tries to convert the new value to match the type of the column.
tdb <- subset(tdb, country != "265")
tdb$population <- as.numeric(tdb$population)
typeof(tdb$population)
## [1] "double"
Exercise 3: Data Filtering
- Use the
tdbdata frame with two countries and three columns (country, population, capital) that we have created:
tdb <- data.frame(
country = c("Switzerland", "Austria"),
population = c(8.3, 8.7),
capital = c("Bern", "Vienna")
)
How can we filter out entries with a population of more than 8.0 million, but less than 8.4 million?
- What exactly does this example do?
tdb[tdb$country == "Switzerland", ]
- (more difficult) Using the
grepl()andnames()functions, can you subset thetdbdata frame to those columns whose names start with the letter “c” (without specifying these column names explicitly)?
Solution
tdb <- data.frame(
country = c("Switzerland", "Austria"),
population = c(8.3, 8.7),
capital = c("Bern", "Vienna")
)
tdb[tdb$population > 8.0 & tdb$population < 8.4, ]
## country population capital
## 1 Switzerland 8.3 Bern
It extracts all records with country name “Switzerland”.
tdb[, grepl("c", names(tdb))]
## country capital
## 1 Switzerland Bern
## 2 Austria Vienna
Exercise 4: Table Design
Imagine that we are conducting a research project that collects data about families. Each family consists of one or more persons living at the same address. The project collects data about each family as a whole (city of residence, total income), but also data about each family member (age, gender). The data should be stored in a tabular database.
Describe two different designs for this database, one using a single table, the second using two tables. What would these two designs looks like? Discuss weaknesses of either approach.
Solution
Single-table solution. One table with the following columns: family_id, city, income, person1age, person1gender, person2age, person2gender, …
Weaknesses: Table needs to grow if families with more persons are added. Empty cells for small families. Family information stored redundantly.
Two-table solution. Families table with the following columns: family_id, city, income. Persons table with the following columns: person_id, gender, age, family_id.
Weaknesses: Need to merge persons and families if we need all the data in a single matrix (for example, for a particular statistical model).