Project 3 - Weather Application Continued

Author
Affiliation

Andrew Moles

Learning Developer, Digital Skills Lab

Published

September 25, 2025

Learning objectives:

  • Create vectors
  • Use calculations on vectors
  • Extract elements from vectors
  • Using boolean indexing on vectors
  • Print out hourly weather reports using iteration

Outcomes

We will continue to write a program that tells us various metrics on what the weather is doing. Your code will automate the process of taking data and making it into a presentable format.

We will be aiming for two outcomes in this project.

Outcome 1 - Make a hourly weather report:

You should end up with a output that iterates through each element of your temperatures and times data. Each line should be an hour with the temperature in Celsius and Fahrenheit (in brackets).

It should also print out what the date at the top of your report.

[1] "Hourly temperatures on: 03/07/2025"
[1] "Temperature at 00: 16.5°C (61.7°F)"
[1] "Temperature at 01: 15.5°C (59.9°F)"
[1] "Temperature at 02: 14.3°C (57.74°F)"
[1] "Temperature at 03: 13.7°C (56.66°F)"
[1] "Temperature at 04: 12.4°C (54.32°F)"
[1] "Temperature at 05: 12°C (53.6°F)"
[1] "Temperature at 06: 12.6°C (54.68°F)"
[1] "Temperature at 07: 14.1°C (57.38°F)"
[1] "Temperature at 08: 16°C (60.8°F)"
[1] "Temperature at 09: 18.2°C (64.76°F)"
[1] "Temperature at 10: 19.7°C (67.46°F)"
[1] "Temperature at 11: 21.1°C (69.98°F)"
[1] "Temperature at 12: 21.8°C (71.24°F)"
[1] "Temperature at 13: 22.6°C (72.68°F)"
[1] "Temperature at 14: 23.4°C (74.12°F)"
[1] "Temperature at 15: 24.3°C (75.74°F)"
[1] "Temperature at 16: 23.3°C (73.94°F)"
[1] "Temperature at 17: 23.6°C (74.48°F)"
[1] "Temperature at 18: 23.1°C (73.58°F)"
[1] "Temperature at 19: 23.4°C (74.12°F)"
[1] "Temperature at 20: 23.4°C (74.12°F)"
[1] "Temperature at 21: 21.6°C (70.88°F)"
[1] "Temperature at 22: 20.7°C (69.26°F)"
[1] "Temperature at 23: 19.5°C (67.1°F)"

Outcome 2 - Finding hours with high temperatures

We can filter our temperature vector, which we can add names to, which tells us which hours had temperatures over 20°C.

You should end up with an output, like below, that has the hour information and the temperature information in one vector.

  11   12   13   14   15   16   17   18   19   20   21   22 
21.1 21.8 22.6 23.4 24.3 23.3 23.6 23.1 23.4 23.4 21.6 20.7 

The data

Using the data below we will be making a weather reporting tool for a hourly report.

Hourly temperatures:

16.5, 15.5, 14.3, 13.7, 12.4, 12, 12.6, 14.1, 16, 18.2, 19.7, 21.1, 21.8, 22.6, 23.4, 24.3, 23.3, 23.6, 23.1, 23.4, 23.4, 21.6, 20.7, 19.5

Date and times:

"03/07/2025 00:00:00", "03/07/2025 01:00:00", "03/07/2025 02:00:00", "03/07/2025 03:00:00", "03/07/2025 04:00:00", "03/07/2025 05:00:00", "03/07/2025 06:00:00", "03/07/2025 07:00:00", "03/07/2025 08:00:00", "03/07/2025 09:00:00", "03/07/2025 10:00:00", "03/07/2025 11:00:00", "03/07/2025 12:00:00", "03/07/2025 13:00:00", "03/07/2025 14:00:00", "03/07/2025 15:00:00", "03/07/2025 16:00:00", "03/07/2025 17:00:00", "03/07/2025 18:00:00", "03/07/2025 19:00:00", "03/07/2025 20:00:00", "03/07/2025 21:00:00", "03/07/2025 22:00:00", "03/07/2025 23:00:00"

Steps to help you get to the outcome

Part 1 - the setup

Open an R script file and save it.

Part 2 - using vectors

Using the data shown above, store the hourly temperatures and date and times data into R as vectors. Note that this data is from a different date to the data we used in project 2.

Part 3 - string manipulation

Extract part of the string, known as substrings, from the date and time data to give you just the date element. Store the result as a variable, keeping just the first element; you should get the result below.

03/07/2025

We want to do this programmatically, so no copy and pasting from the data, also known as “hardcoding”. This will help us to automate our code if we want to use different data.

Part 4 - more string manipulation

Extract part of the string from the date and time data to give you just the hour elements. Store this as a new vector.

Part 5 - convert temperatures

Convert the hourly temperatures data to Fahrenheit and store the result as a new vector.

Part 6 - start making the text output

Our ultimate task is to iterate through our temperature and date data to print out a message telling us the temperature for each hour. To get there it is sometimes helpful to breakdown the process.

Try first to a make an output for just the first hour, like the result below, using vector indexing to get the data you need. Then use string concatenation to bring all the information together to create a text output.

Temperature at 00: 16.5°C (61.7°F)

Task 7 - finish the text output

Now using the code you just wrote, iterate your code so you get the hourly weather report with both Celsius and Fahrenheit, as seen in Section 2.1.

Task 8 - naming a vector

Using vector naming, add the hours vector as a name to the temperature vector.

Part 9 - filtering the temperature vector

Using boolean indexing, filter the temperature vector so we are only left with temperatures over 20°C. Your outcome should be the same as seen in Section 2.2.

Part 10 - test your code with new data

A good way to test out your code is to try it on different data.

Below we have the temperature data for a few days after the data we have been using. Using the steps you did previously, write a weather report using this new data.

Hourly temperatures:

20.8, 19.6, 18.7, 17.8, 17.7, 19.1, 21.4, 22.8, 24.1, 26.5, 27.8, 29.4, 30.7, 31.8, 32.4, 32.0, 31.8, 31.5, 31.2, 29.9, 28.3, 27.1, 26.2

Date and times:

"30/06/2025 01:00:00", "30/06/2025 02:00:00", "30/06/2025 03:00:00", "30/06/2025 04:00:00", "30/06/2025 05:00:00", "30/06/2025 06:00:00", "30/06/2025 07:00:00", "30/06/2025 08:00:00", "30/06/2025 09:00:00", "30/06/2025 10:00:00", "30/06/2025 11:00:00", "30/06/2025 12:00:00","30/06/2025 13:00:00", "30/06/2025 14:00:00", "30/06/2025 15:00:00", "30/06/2025 16:00:00", "30/06/2025 17:00:00", "30/06/2025 18:00:00","30/06/2025 19:00:00", "30/06/2025 20:00:00", "30/06/2025 21:00:00", "30/06/2025 22:00:00", "30/06/2025 23:00:00"

Final task - fill out the survey!

We are always looking to improve and iterate our workshops. Follow the link to give your feedback.