Question
Using R: functions and loops Background: Ozone concentration is an important component of air quality. It is monitored frequently and at many locations; these results
Using R: functions and loops Background: Ozone concentration is an important component of air quality. It is monitored frequently and at many locations; these results are subject to US federal regulations. The standards are based on running 8h averages of ozone according to US EPA procedure [1]. In a day (24 h) period, there are 24 hourly values of ozone. To be able to obtain a running 8h average for each hour that do not overlap with 8h average of the previous or next day, we use only 24- 8+1=17 hours to start the average. For example, from 00:00 to 07:00, from 01:00 to 08:00, from 02:00 to hour 09:00, and so on until the period from 16:00 to hour 23:00. This may vary, it is also done starting at 7 am of one day and ending at 6 am of the next day [2]. Ozone values are given by three decimal places in ppm using truncation. Ozone results are truncated to three decimal places by US federal regulatory standards, but it is rounded by some specific state standards. For this problem, we will use files data/ozone24hsample1.txt and data/ozone24hsample2.txt. These are not real data. Questions: 1. (2 points) Inspect both files using the Notepad++ and embed figures with a screen capture of the first 10 lines. How many lines of data do we have in each file? Study the time format. How often was ozone sampled? 2. Read each file to a data frame. Use x1 and x2 for sample 1 and x2 for sample 2. 3. Start to write a function named oz8h with argument x corresponding to these objects. Use curly braces as placeholders for now. We will complete the function adding codes as you respond to the following questions. 4. Create a matrix named x8 to store results in 17 columns and 8 rows. 5. Use mx8 <- array() to create a column vector to store the 8h average
6. Now add a loop with the following code and explain what it does
for(i in 8:24){ j <- i-8+1 # starts at 1 ends at 24-8+1=17 x8[,j] <- x[j:i,2] # truncating mx8[j] <- trunc(mean(x8[,j])*1000)/1000 } 7. Add a line to calculate maximum maxx8 <- max(mx8) and add a line to return values from the function before closing return(list(avg.8h=mx8, max.avg.8h=maxx8)) . Note: this is how you return multiple results. 8. Source the function and run it for x1 and x2. That is oz8h(x1) and oz8h(x2). You may have to troubleshoot and debug the function.
9. Show the result of your function for both x1 and x2 and explain how the results match the return code you used above.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started