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<- a + a b + a == b a !!a sqrt(2) # see ?sqrt sqrt(2)^2 sqrt(2)^2 == 2 # Why? # Hint: Compute the difference sqrt(2)^2 - 2 / O / 0 # (using o and O from above) o 0 / (o * O) 0 / (o * 0) + b + C a sum(a, b) - sum(a + b) :a # divide b by a? blength(b:a) <- i + 1 i nchar(d) - length(d) e+ e + !!e e <- stuff e paste(d, e)
1.8.2 Exercise 2
Fun with plot functions
In Section 1.2.2, 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.
- 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
- 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
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_2() functions of the same package.
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()function (of the ds4psy package) by first calling it a few times (with and without arguments). Then study its documentation (by calling
What are the differences between the
Bonus task:23 Use the base R function
sample()to sample from the numbers
sample()yields a fair dice in which all six numbers occur equally often, and
sample()yields a biased dice in which the value
6occurs twice as often as any other number.
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
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.5 Exercise 5
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.6 Exercise 6
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
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.8 Exercise 8
Exploring participant data
Explore the participant information of
p_info (Woodworth, O’Brien-Malone, Diamond, & Schüz, 2018) 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)?
Bonus task: The variables of
p_infoare stored as numeric variables, but some could also be factors.
Which of the variables could or should be turned into factors?
It seems that
incomecould/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.
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.
Bonus in the sense of challenging, but rewarding when you solve it.↩︎