The following exercises require the essential tibble commands and repeat many commands from earlier chapters (involving dplyr and ggplot2).
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 three iris species (called setosa, versicolor, and virginica).
?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 four measurement columns (
Petal.Width) for each of the three
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 three facets) for each species.
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.
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: <- ds4psy::posPsy_wide # from ds4psy package posPsy_wide # 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: <- ds4psy::posPsy_long # from ds4psy package posPsy_long # 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 five occasions (with means rounded to one decimal):
In case this table is too wide to be displayed in full, here is how it looks in the Console:
#> # A tibble: 4 × 7 #> intervention mn_cesd_0 mn_cesd_1 mn_cesd_2 mn_cesd_3 mn_cesd_4 mn_cesd_5 #> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 1 15.1 15.3 13.6 12 11.2 13.5 #> 2 2 16.2 14.6 11.4 12.5 13.4 14.6 #> 3 3 16.1 12.3 14.8 13.9 14.9 13 #> 4 4 12.8 9.9 9.5 9.1 7.7 10.2
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
posPsy_...datasets (using 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 a
ggplot() call, use a data file that is better suited for this purpose.
Why can’t you just directly plot
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 two datasets (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 (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: <- ds4psy::falsePosPsy_all # from ds4psy package falsePosPsy_all # 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.