Loops temperature sensory analysis (solution)

NoteExercise

You have deployed multiple temperature sensors at different locations to monitor daily temperature patterns. Each sensor records temperature readings every hour for a 24-hour period. The data from each sensor is stored in a separate CSV file with the naming pattern sensor-X-temp.csv where X is the sensor number.

  • If temperature_sensor_data.zip is not already in your working directory download the zip file using download.file()
  • Unzip it using unzip()
  • Obtain a list of all of the files with file names matching the pattern "sensor-*-temp.csv" (using list.files())

Each file contains two columns:

  • hour: Hour of the day (0-23)
  • temperature: Temperature reading in Celsius
  1. Use a loop to load each sensor data file and calculate the mean temperature for the sensor. Store the results in a vector called mean_temps. After the loop display the completed vector.

  2. Create a copy of your code from (1) and modify it to also find the maximum temperature recorded by each sensor and the temperature range (difference between maximum and minimum temperature) for each sensor. Store these values in vectors called max_temps, and temp_ranges. After the loop display the completed vectors.

  3. Create an empty data frame to store all your results and then write a loop to determine the following values for each file and store them in the data frame:

  • sensor_file: The filename of the sensor data
  • mean_temp: Mean temperature for that sensor
  • max_temp: Maximum temperature recorded
  • min_temp: Minimum temperature recorded
  • temp_range: Temperature range (max - min)
  1. Challenge (optional) Extend your analysis to find the hour when each sensor recorded its highest temperature. Add a column called peak_hour to your results data frame and display the data frame.

    :::

CautionOutput solution

Part 1


Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
[1] 20.05833 21.90833 18.10000 21.05417 18.97083

Part 2

[1] 28.0 31.7 25.5 30.0 25.0
[1] 16.0 20.2 14.7 18.3 12.5

Part 3

        sensor_file mean_temp max_temp min_temp temp_range
1 sensor-1-temp.csv  20.05833     28.0     12.0       16.0
2 sensor-2-temp.csv  21.90833     31.7     11.5       20.2
3 sensor-3-temp.csv  18.10000     25.5     10.8       14.7
4 sensor-4-temp.csv  21.05417     30.0     11.7       18.3
5 sensor-5-temp.csv  18.97083     25.0     12.5       12.5

Part 4

        sensor_file mean_temp max_temp min_temp temp_range peak_hour
1 sensor-1-temp.csv  20.05833     28.0     12.0       16.0        15
2 sensor-2-temp.csv  21.90833     31.7     11.5       20.2        15
3 sensor-3-temp.csv  18.10000     25.5     10.8       14.7        15
4 sensor-4-temp.csv  21.05417     30.0     11.7       18.3        13
5 sensor-5-temp.csv  18.97083     25.0     12.5       12.5        14