## 1.8 Exercises The following exercises allow you to apply the basic R concepts and commands introduced in this chapter.

### 1.8.1 Exercise 1

#### R objects and assignments

Check the Environment tab of RStudio to see which objects are currently defined to which values (after working through this chapter). Then evaluate and explain the following expressions (and correct any errors that may occur):

# Note: The following assume the object definitions from above.
a
b
b <- a + a
a + a == b
!!a

sqrt(2)  # see ?sqrt
sqrt(2)^2
sqrt(2)^2 == 2  # Why?
# Hint: Compute the difference sqrt(2)^2 - 2

o / O / 0   # (using o and O from above)
0 / (o * O)
0 / (o * 0)

a + b + C

sum(a, b) - sum(a + b)

b:a  # divide b by a?
length(b:a)

i <- i + 1

nchar(d) - length(d)

e
e + e + !!e

e <- stuff
paste(d, e)

### 1.8.2 Exercise 2

#### Fun with plot functions

In Section 1.2.5, we explored the plot_fn() function of the ds4psy package to discover the meaning of its arguments. In this exercise, we will explore the plot_fun() function of the same package.

1. Assume the perspective of an empirical scientist to explore and decipher the arguments of the plot_fun() function in a similar fashion.
library(ds4psy)  # loads the package

plot_fun()  # calls the function (with default arguments) Hint: Solving this task essentially means to answer the question “What does this argument do?” for each argument (i.e., the lowercase letters from a to f, and c1 and c2).

1. Use your exploration of plot_fun() to reconstruct the command that creates the following plots:  Hint: Check the documentation of plot_fun() (e.g., for color information).

### 1.8.3 Exercise 3

#### Dice sampling

In Section 1.6.4, we explored the coin() function of the ds4psy package and mimicked its functionality by the sample() function. In this exercise, we will explore the dice() and dice_2() functions of the same package.

1. Explore the dice() function (of the ds4psy package) by first calling it a few times (with and without arguments). Then study its documentation (by calling ?dice()).

2. Explore the dice_2() function (of the ds4psy package) by first calling it a few times (with and without arguments). Then study its documentation (by calling ?dice_2()).
What are the differences between the dice() and dice_2() functions?

3. Bonus task:29 Use the base R function sample() to sample from the numbers 1:6 so that

1. sample() yields a fair dice in which all six numbers occur equally often, and

2. sample() yields a biased dice in which the value 6 occurs twice as often as any other number.

Hint: The prob argument of sample() can be set to a vector of probability values (i.e., as many values as length(x) that should sum up to a total value of 1).

### 1.8.4 Exercise 4

#### Cumulative savings

With only a little knowledge of R you can perform quite fancy financial arithmetic. Assume that you have won an amount a of EUR 1000 and are considering to deposit this amount into a new bank account that offers an annual interest rate int of 0.1%.

1. How much would your account be worth after waiting for n = 2 full years?

2. What would be the total value of your money after n = 2 full years if the annual inflation rate inf is 2%?

3. What would be the results to 1. and 2. if you waited for n = 10 years?

Answer these questions by defining well-named objects and performing simple arithmetic computations on them.

Note: Do not worry if you find this task difficult at this point — we will revisit it later. In Exercise 6 of Chapter 12: Iteration, we will use loops and functions to solve it in a more general fashion.

### 1.8.5 Exercise 5

#### Vector arithmetic

When introducing arithmetic functions above, we showed that they can be used with numeric scalars (i.e., numeric objects with a length of 1).

1. Demonstrate that arithmetic functions also work with two numeric vectors x and y (of the same length).

2. What happens when the vectors x and y have different lengths?

Hint: Define some numeric vectors and use them as arguments of various arithmetic functions. To better understand the behavior in 2., look up the term “recycling” in the context of R vectors.

### 1.8.6 Exercise 6

#### Cryptic arithmetic

Predict the result of the arithmetic expression x %/% y * y + x %% y. Then test your prediction by assigning some number to x and to y and evaluating the expression. Finally, explain why the result occurs.

### 1.8.7 Exercise 7

#### Survey age

Assume the following definitions for a survey:

• A person with an age from 1 to 17 years is classified as a minor;

• a person with an age from 18 to 64 years is classified as an adult;

• a person with an age from 65 to 99 years is classified as a senior.

Generate a vector with 100 random samples that specifies the age of 100 people (in years), but contains exactly 20 minors, 50 adults, and 30 seniors.

Now use some functions on your age vector to answer the following questions:

1. What is the average (mean), minimum, and maximum age in this sample?

2. How many people are younger than 25 years?

3. What is the average (mean) age of people older than 50 years?

4. How many people have a round age (i.e., an age that is divisible by 10)? What is their mean age?

### 1.8.8 Exercise 8

#### Exploring participant data

Explore the participant information of p_info by describing each of its variables:

1. How many individuals are contained in the dataset?

2. What percentage of them is female (i.e., has a sex value of 1)?

3. How many participants were in one of the 3 treatment groups (i.e., have an intervention value of 1, 2, or 3)?

4. What is the participants’ mean education level? What percentage has a university degree (i.e., an educ value of at least 4)?

5. What is the age range (min to max) of participants? What is the average (mean and median) age?

6. Describe the range of income levels present in this sample of participants. What percentage of participants self-identifies as a below-average income (i.e., an income value of 1)?

7. Bonus task: The variables of p_info are stored as numeric variables, but some could also be factors.

• Which of the variables could or should be turned into factors?
It seems that intervention, sex, educ and income could/should be turned into factors.

• Recode some variables as factors (by consulting the codebook in Section 1.6.1).

• Verify that the recoded factors correspond to the original variables.

Hint: The p_info data was defined and described above (in Section 1.6.1). To allow comparisons with the original data, create a copy p_data and recode the columns of the copy, rather than the original. You can also use the labels argument of factor() to encode a character label for each factor level.

This concludes our first set of exercises on basic R concepts and commands.

### References

Woodworth, R. J., O’Brien-Malone, A., Diamond, M. R., & Schüz, B. (2018). Data from ‘Web-based positive psychology interventions: A reexamination of effectiveness’. Journal of Open Psychology Data, 6(1). https://doi.org/10.5334/jopd.35

1. Bonus in the sense of challenging, but rewarding when you solve it.↩︎