Skip to content

Commit

Permalink
Add material on lapply with files and dataframes
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanwhite committed Oct 25, 2024
1 parent 436c85c commit a9c9474
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions materials/iteration-without-loops-R.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,26 @@ plant_data |>
summarize(biomass = get_biomass(volumes))
```

#### Using lapply with files and data frames (**optional**)

```r
library(readr)

get_unique_locations <- function(df){
unique_locations <- df |>
select(lat, long) |>
distinct()
return(unique_locations)
}

download.file("https://www.datacarpentry.org/semester-biology/data/locations.zip",
"locations.zip")
unzip("locations.zip")
data_files = list.files(pattern = "locations-")
data_frames = lapply(data_files, read_csv)
unique_locations <- lapply(data_frames, get_unique_locations)
```

#### Other apply functions (**optional**)

* There are a few other apply functions
Expand Down

0 comments on commit a9c9474

Please sign in to comment.