1.8 Exercises

ds4psy: Exercises 1

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

1.8.1 Exercise 1

Creating and changing R objects

Our first exercise (on Section 1.2) begins by cleaning up our current working environment and then defines, evaluates, and changes some R objects.

  1. Cleaning up: Check the Environment tab of the RStudio IDE to see which objects are currently defined to which values (after working through this chapter). Then run rm(list = ls()) and explain what happens (e.g., by reading the documentation of ?rm).

Note that running rm(list = ls()) will issue no warning, so we must only ever use this when we no longer need the objects currently defined (i.e., when we want to start with a clean slate).

  1. Creating R objects: Create some new R objects by evaluating the following assignment expressions:
a <- 100
b <- 2
d <- "weird"
e <- TRUE
o <- FALSE
O <- 5
  1. Evaluating and changing R objects: Given this set of new R objects, evaluate the following expressions and explain their results (correcting for any errors that may occur):
a
b
c <- a + a 
a + a == c

!!a            
as.logical(a)

sqrt(b)         
sqrt(b)^b       
sqrt(b)^b == b  

o / O
o / O / 0

o <- "ene mene mu"
o / O / 0

o <- FALSE 
o / O / 0   

a + b + C

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

b:a  

i <- i + 1  

nchar(d) - length(d)  

e  
e + e + !!e
e <- stuff
paste(d, e)

1.8.2 Exercise 2

Plotting fun

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 we can perform quite fancy financial arithmetic. Assume that we 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 our account be worth after waiting for \(n = 2\) full years?

  2. What would be the total value of our money after \(n = 2\) full years if the annual inflation rate inf was 2%?

  3. What would be the results to 1. and 2. if we waited for \(n = 10\) years?

  4. A low interest rate of 0.1% was common in Europe from 2016 to 2020. Contrast this with the situation in 2023, in which the key ECB interest rate is 4.5%, but with an annual inflation of around 5%.

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

Note: Solving these tasks in R requires defining some numeric objects (e.g., a, int, and inf) and performing arithmetic computations with them (e.g., using +, *, ^, with appropriate parentheses).
Do not worry if you find these tasks difficult at this point — we will revisit them later. In Exercise 6 of Chapter 12: Iteration, we will use loops and functions to solve such tasks 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.

  • Test your prediction by assigning some positive integer(s) to x and to y and evaluating the expression.

  • What changes, when either x or y is a vector containing more than one positive integer?

  • Finally, explain why the result occurs.

1.8.7 Exercise 7

Survey age

Assume that a survey classifies individuals’ age values according to the following definitions:

  • 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 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 (Woodworth et al., 2018) 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). As it is also included as a tibble posPsy_p_info in the ds4psy package, it can be obtained by (re-)assigning:

p_info <- ds4psy::posPsy_p_info

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.↩︎