The following exercises require the essential tibble commands and repeat many commands from earlier chapters (involving dplyr and ggplot2).
5.4.1 Exercise 1
iris data — contained in R datasets — into a tibble and conduct an EDA on it.
iris provides the measurements (in cm) of plant parts (length and width of sepal and petal parts) for 50 flowers from each of 3 iris species (called setosa, versicolor, and virginica). (Evaluate
?iris to obtain a description of the dataset.)
datasets::irisas a tibble
irthat contains this data and inspect it. Are there any missing values?
Compute a summary table that shows the means of the 4 measurement columns (
Petal.Width) for each of the 3
Species(in rows). Save the resulting table of means as a tibble
Create a histogram that shows the distribution of
Sepal.Widthvalues across all species.
Create a plot that shows the shape of the distribution of
Sepal.Widthvalues for each species.
Create a plot that shows
Petal.Widthas a function of
Sepal.Widthseparately (i.e., in 3 facets) for each species.
5.4.2 Exercise 2
Anna, Brian, and Caro are sharing a flat and keep a record of the items that each of them purchased for the household. At the end of each week, they use this data to balance their account. As an aspiring data scientist, you offer your help. Here’s last week’s data:
|Anna||Bread: $2.50||Pasta: $4.50||Pencils: $3.25||Milk: $4.80||–||Cookies: $4.40||Cake: $12.50|
|Butter: $2.00||Cream: $3.90|
|Brian||Chips: $3.80||Beer: $11.80||Steak: $16.20||–||Toilet paper: $4.50||–||Wine: $8.80|
|Caro||Fruit: $6.30||Batteries: $6.10||–||Newspaper: $2.90||Honey: $3.20||Detergent: $9.95||–|
Which variables and which observations would you define here? Go ahead and enter the data into a tibble
acc_1to answer the following questions (by using dplyr for creating tables that contain the answer):
- How much money was spent this week?
- Which percentage of the overall amount was spent by each person?
- How many items did each person purchase?
- How much did each person pay overall?
- Who buys the cheapest/most expensive items (on average)?
- How much is being spent on each day of the week (overall and on average)?
- What is the order of days sorted by the overall amount spent (from most expensive to least expensive)?
Interpret and re-create the following graphs (using ggplot2 and possibly dplyr):
- Bonus task: What do the following plots show? Try re-creating the plots from the data in
Hint: These plots are created with
geom_area. However, rather than directly calling
ggplot, consider first using dplyr to transform the data of
acc_1 into summary tables that contain the values needed for the plots. You may have to combine multiple
mutate commands to compute all required variables.
5.4.3 Exercise 3
Positive psychology tibbles
In this exercise, we will enter some results from the Exploring data chapter (Chapter 4) as a tibble. Next, we re-compute the results from the raw data and visualize the results.
To do this exercise, re-load the following data files (into R objects
# Load data: posPsy_wide <- ds4psy::posPsy_wide # from ds4psy package # posPsy_wide <- readr::read_csv(file = "http://rpository.com/ds4psy/data/posPsy_data_wide.csv") # online dim(posPsy_wide) # 295 294 #>  295 294 # 3. Corrected DVs in long format: posPsy_long <- ds4psy::posPsy_long # from ds4psy package # posPsy_long <- readr::read_csv(file = "http://rpository.com/ds4psy/data/posPsy_AHI_CESD_corrected.csv") # online dim(posPsy_long) # 990 x 50 #>  990 50
The following table shows the mean depression scores per intervention for each of the 5 occasions (with means rounded to 1 decimal):
Enter this data directly into a tibble
my_tbl(by using either the
Re-compute an identical tibble
my_tbl_2by transforming one of the
dplyr) and verify that
Visualize the information expressed by
my_tblin a transparent way (e.g., by creating a bar or line plot).
Hint: If this is difficult by using
data = my_tbl in
ggplot, use a data file that is better suited for this purpose. Why can’t you just directly plot
5.4.4 Exercise 4
Having considered the benefits of positive psychology, we can now consider the pitfalls of false-positive psychology: Presenting incidential and irrelevant results as statistically significant findings. An intriguing article on this phenomenon reports noteworthy results based on 2 datasets (J. P. Simmons, Nelson, & Simonsohn, 2011):
- Simmons, J.P., Nelson, L.D., & Simonsohn, U. (2011). False-positive psychology: Undisclosed flexibility in data collection and analysis allows presenting anything as significant. Psychological Science, 22(11), 1359–1366. doi: https://doi.org/10.1177/0956797611417632
The data was published separately (J. Simmons, Nelson, & Simonsohn, 2014) and pre-processed to facilitate working with it. (See Section B.2 of Appendix B for details on the data and corresponding articles.)
The following table was created by summarizing the data of both studies. It reports the mean, minimum, and maximum age of participants per condition
cond, as well as the number of people in those conditions who reported to feel a certain age (from very young to very old):
Enter this data as a tibble
tbl_1(by using the
Import the original dataset (either from the ds4psy package or from http://rpository.com/ds4psy/data/falsePosPsy_all.csv) and re-create the data of
tbl_1from the original data as a new tibble
tbl_org(by using dplyr).
# Import the dataset: falsePosPsy_all <- ds4psy::falsePosPsy_all # from ds4psy package # falsePosPsy_all <- readr::read_csv("http://rpository.com/ds4psy/data/falsePosPsy_all.csv") # online
Visualize the following aspects of the data (by using ggplot2):
Use the tibble
tbl_orgto plot the number of participants per condition (e.g., as a bar plot).
Plot the mean age per condition with the minimum and the maximum age (e.g., by using
This concludes our set of exercises on tibbles.
Simmons, J. P., Nelson, L. D., & Simonsohn, U. (2011). False-positive psychology: Undisclosed flexibility in data collection and analysis allows presenting anything as significant. Psychological Science, 22(11), 1359–1366. https://doi.org/10.1177/0956797611417632
Simmons, J., Nelson, L., & Simonsohn, U. (2014). Data from paper “False-Positive Psychology: Undisclosed Flexibility in Data Collection and Analysis Allows Presenting Anything as Significant”. Journal of Open Psychology Data, 2(1). https://doi.org/10.5334/jopd.aa