Skip to content

Commit 245bafa

Browse files
committed
Updates to second lesson (.md)
1 parent a5b46f0 commit 245bafa

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ See /scripts/curriculum.Rmd
987987
988988
`apply()` lets you apply an arbitrary function over a collection. This is an example of a higher-order function (map, apply, filter, reduce, fold, etc.) that can (and should) replace loops for most purposes. They are an intermediate case between vectorized operations (very fast) and for loops (very slow). Use them when you need to build a new collection and vectorized operations aren't available.
989989

990-
### `apply()`: Apply a function over the margins of an array
990+
### (Optional) `apply()`: Apply a function over the margins of an array
991991

992992
``` r
993993
m <- matrix(1:28, nrow = 7, byrow = TRUE)
@@ -1002,6 +1002,7 @@ apply(m, 2, sum)
10021002

10031003
``` r
10041004
lst <- list(title = "Numbers", numbers = 1:10, data = TRUE)
1005+
str(lst)
10051006

10061007
## length() returns the length of the whole list
10071008
length(lst)
@@ -1020,7 +1021,7 @@ sapply(lst, length)
10201021
sapply(lst, length, simplify = FALSE)
10211022
```
10221023

1023-
### Use `apply` and friends to extract nested data from a list
1024+
### (Optional) Use `apply` and friends to extract nested data from a list
10241025

10251026
1. Read a file JSON into a nested list
10261027

@@ -1176,7 +1177,27 @@ north_america <- c("Canada", "Mexico", "United States")
11761177
gdp <- gapminder$pop * gapminder$gdpPercap
11771178
```
11781179

1179-
2. Write a function to perform a total GDP calculation on a filtered subset of your data.
1180+
2. Write a function to perform a total GDP calculation on a filtered subset of your data. Begin with the minimal working function.
1181+
1182+
``` r
1183+
calcGDP <- function(df, year, country) {
1184+
# Note that year and country are vectors
1185+
# Get rows that match year
1186+
df <- df[df$year %in% year, ]
1187+
1188+
# Get subset of year rows that match country
1189+
df <- df[df$country %in% country,]
1190+
1191+
# Calculate total GDP
1192+
gdp <- df$pop * df$gdpPercap
1193+
1194+
# Return new data frame with GDP column
1195+
new_df <- cbind(df, gdp=gdp)
1196+
return(new_df)
1197+
}
1198+
```
1199+
1200+
3. (Optional) Add defensive programming elements
11801201

11811202
``` r
11821203
calcGDP <- function(df, year=NULL, country=NULL) {
@@ -1193,7 +1214,7 @@ north_america <- c("Canada", "Mexico", "United States")
11931214
}
11941215
```
11951216

1196-
3. Mutating `df` inside the function doesn't affect the global `gapminder` data frame (because of pass-by-value and scope).
1217+
4. Mutating `df` inside the function doesn't affect the global `gapminder` data frame (because of pass-by-value and scope).
11971218
11981219
### **Challenge 6**: Testing and debugging your function
11991220
@@ -1227,7 +1248,7 @@ See data/curriculum.Rmd
12271248
}
12281249
```
12291250

1230-
3. Version 2: Bypass `calcGDP` function
1251+
3. (Optional) Version 2: Bypass `calcGDP` function
12311252

12321253
``` r
12331254
for (year in unique(gapminder$year)) {

0 commit comments

Comments
 (0)