Skip to content

Commit b1c34ac

Browse files
committed
Updated code examples
1 parent 290134e commit b1c34ac

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

scripts/curriculum.Rmd

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,18 @@ mutate()
148148

149149
Create a data frame that has the African values for `lifeExp`, `country`, and `year`.
150150

151-
152151
### Challenge 8
153152

154-
1. Calculate the average life expectancy in 2002 of 2 randomly selected countries for each continent.
155-
2. Then arrange the continent names in reverse order.
153+
1. Calculate the average life expectancy in 2002 of 2 randomly selected countries for each continent.
154+
2. Then arrange the continent names in reverse order.
156155

157156
Hint: Use the dplyr functions `arrange()` and `sample_n()`. They have similar syntax to other dplyr functions.
158157

159-
160158
## Tidyverse in the wild
161159

162160
![Highest single contributors to increased mortality by age group, gender, and race between 2018/2019 average and 2020 totals for younger people](../images/nri_tidyverse.png)
163161

164-
Credit: https://twitter.com/NewRiverInvest/status/1538984422518599680
162+
Credit: <https://twitter.com/NewRiverInvest/status/1538984422518599680>
165163

166164
```{r tidyverse_example}
167165

scripts/examples.R

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,53 @@ for (year in unique(gapminder$year)) {
7171
}
7272

7373
## --- Aside: Get list of files in subdirectory ---
74-
dir(path = "../data", pattern = "north_america_[1-9]*.csv")
74+
#dir(path = "../data", pattern = "north_america_[1-9]*.csv")
75+
dir(path = "../data", pattern = "gapminder_gdp.*.csv")
7576

7677
## --- Read files using a for loop ---
7778
# Create an empty list to hold your input
7879
df_list <- list()
7980

8081
# Get list of files to read
81-
file_names <- dir(path = "../data", pattern = "north_america_[1-9]*.csv")
82+
file_names <- dir(path = "../data", pattern = "gapminder_gdp.*.csv")
8283

84+
# Read files into data frames
8385
for (f in file_names){
8486
df_list[[f]] <- read.csv(file = file.path("../data", f))
8587
}
8688

89+
# Check data frame dimensions
90+
for (name in names(df_list)) {
91+
print(name)
92+
print(dim(df_list[[name]]))
93+
}
94+
95+
# What's going on with Americas?
96+
for (name in names(df_list)) {
97+
print(name)
98+
print(dim(df_list[[name]]))
99+
print(colnames(df_list[[name]]))
100+
}
101+
102+
# Drop the continent column for Americas
103+
americas <- df_list[["gapminder_gdp_americas.csv"]]
104+
df_list[["gapminder_gdp_americas.csv"]] <- americas[, ! colnames(americas) %in% c("continent")]
105+
106+
# Number of columns don't match
107+
df <- do.call(rbind, df_list)
108+
87109
## --- Read files using apply ---
88110
file_names <- dir(path = "../data", pattern = "north_america_[1-9]*.csv")
89111
df_list <- lapply(file.path("../data", file_names), read.csv)
90-
91-
names(df_list)
92-
df_list[[1]]
93112
names(df_list) <- file_names
94113

114+
95115
## --- Read files using apply with pipes ---
96116

97117
## Vectorize version with pipes
98118
df_list <- file.path("../data", file_names) |>
99119
lapply(read.csv)
100120

101-
102121
##-------------------------------------------------
103122
## Challenges
104123
##-------------------------------------------------

0 commit comments

Comments
 (0)