Loops

Multi-file Analysis


You have a satellite collars on a number of different individuals and want to be able to quickly look at all of their recent movements at once. The data is posted daily to a url as a zip file that contains one csv file for each individual: /UCSB-Smithsonian-R-workshop/data/individual_collar_data.zip Start your solution by:

  1. Use a loop to load each of these files into R and make a line plot (using geom_path) for each file with long on the x axis and lat on the y axis. Graphs, like other types of output, won’t display inside a loop unless you explicitly display them, so you need put your ggplot command inside a print statement. Include the name of the file in the graph as the graph title using labs.

  2. Add code to the loop to calculate the minimum and maximum latitude in the file, and store these values, along with the name of the file, in a data frame. When you create the empty data frame you’ll need to include stringsAsFactors = FALSE to prevent the character column for the file name becoming a factor. Show the data frame as output.

If you’re interested in seeing another application of for loops, check out the code used to simulate the data for this exercise using for loops.

[click here for output] [click here for output] [click here for output] [click here for output] [click here for output] [click here for output] [click here for output] [click here for output] [click here for output]