Chapter 12 For loops (and other skills)
Here we are going to learn how to write a for loop to download some data and name files. We will do two sites for simplicity, but this can be fed as many sites as you wish!
12.2 Defining parameters
You could type out the sites and names or you could pull it from some whatNWISsites call.
sites <- c("06037500", "12363000")
names <- c("salmon", "flathead")
params <- c("00060", "00010") # This is temperature and Q. You can obviously change and then also change the readNWISdata to something else and the parameters, etc.
start <- as_date("2020-10-01") # Just a nice round number for start time.
end <- as_date("2021-09-30") #End of water year.
service <- "dv" # Daily values
# Removed those sites for testing
# "12363000", "13296000", "12344000", "12447200", "13331500", "06191500"Instead of writing a function, we use for(i in sites). So this will run through the list of sites and download the data and do whatever else you tell it. Then we create a filename that is data_, i. So it will write out data_06037500.csv for the first, data_12363000.csv for the second, so on. Then I just read the .csv in to check and make sure it worked.
# for (i in sites) {
# tmp <- readNWISdata(sites = i, parameterCd = params, service = service, startDate = start, endDate = end) %>%
# renameNWISColumns()
# filename <- paste("data_", i, sep="")
# write_csv(tmp, file = paste("data/", filename, ".csv", sep=""))
# }
# test_df <- read_csv("data/data_06037500.csv")Now we can add what is referred to as a “zipper” or “zippering” where we would name each file not by site but by name. See here:
# for (k in seq_along(sites)) {
# tmp <- readNWISdata(
# sites = sites[k],
# parameterCd = params,
# service = service,
# startDate = start,
# endDate = end
# ) %>%
# renameNWISColumns()
#
# filename <- paste0(names[k], ".csv")
#
# write_csv(tmp, file = paste0("data/", filename))
# rm(tmp)
# }
# You can also tell R to load (read_csv) all data files in your folder (e.g., "data/bunch_of_files.csv"). If interested try to get that to work before seeing the end of this Rmd. We could also do this directly to the global environment.
# for (k in seq_along(sites)) {
# tmp <- readNWISdata(
# sites = sites[k],
# parameterCd = params,
# service = service,
# startDate = start,
# endDate = end
# ) %>%
# renameNWISColumns()
#
# assign(paste0(names[k]), tmp)
# rm(tmp)
# }Loading a list of files