The following exercises allow you to apply the basic R concepts and commands introduced in this chapter.
1.8.1 Exercise 1
- Check the Environment tab of R Studio 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)
- In Section 1.2.2, we explored the
plot_fn()function of the ds4psy package to discover the meaning of its arguments. Assume the perspective of an empirical scientist to explore and decipher the arguments of the
plot_fun()function in a similar fashion.
Hint: Solving this task essentially means to answer the question “What does this argument do?”
for each argument (i.e., the lowercase letters from
- 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.2 Exercise 2
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%.
How much would your account be worth after waiting for
n = 2full years?
What would be the total value of your money after
n = 2full years if the annual inflation rate
What would be the results to 1. and 2. if you waited for
n = 10years?
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.3 Exercise 3
When introducing arithmetic functions above, we showed that they can be used with numeric scalars (i.e., numeric objects with a length of 1).
Demonstrate that the same arithmetic functions also work with 2 numeric vectors
y(of the same length).
What happens when
yhave different lengths?
1.8.4 Exercise 4
Predict the result of the arithmetic expression
x %/% y * y + x %% y.
Then test your prediction by assigning some number to
y and evaluating the expression. Finally, explain why the result occurs.
1.8.5 Exercise 5
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:
What is the average (mean), minimum, and maximum age in this sample?
How many people are younger than 25 years?
What is the average (mean) age of people older than 50 years?
How many people have a round age (i.e., an age that is divisible by 10)? What is their mean age?
1.8.6 Exercise 6
Examine the participant information in
p_info (Woodworth et al., 2018) — which is available as
posPsy_p_info in the ds4psy package (or can be imported from a corresponding CSV file at http://rpository.com/ds4psy/data/posPsy_participants.csv) — by describing each of its variables:
How many individuals are contained in the dataset?
What percentage of them is female (i.e., has a
sexvalue of 1)?
How many participants were in one of the 3 treatment groups (i.e., have an
interventionvalue of 1, 2, or 3)?
What is the participants’ mean education level? What percentage has a university degree (i.e., an
educvalue of at least 4)?
What is the age range (
max) of participants? What is the average (mean and median) age?
Describe the range of
incomelevels present in this sample of participants. What percentage of participants self-identifies as a below-average income (i.e., an
incomevalue of 1)?
This concludes our first set of exercises on base R.
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