Temperature Sensors (Loops)
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.zipis not already in your working directory download the zip file usingdownload.file() - Unzip it using
unzip() - Obtain a list of all of the files with file names matching the pattern
"sensor-"(usinglist.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 datamean_temp: Mean temperature for that sensormax_temp: Maximum temperature recordedmin_temp: Minimum temperature recordedtemp_range: Temperature range (max - min)
4. 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.