# Average over three values repeatedly in a column of a dataframe taking into account multiple spots

Hi guys,

I have a dataframe with information related to ozone and I need to calculate a stastistic parameter for a single column. This column has the number of days over a year when the daily maximum rolling 8-hour mean ozone concentration is >120 ug/m3 for multiple sites (stations of air pollutants measurement). Using this column, I need to calculate the average for three years taking into account the different sites.

I don't know how to code this, I have think of a loop, but I have no idea.

A reproducible example, called a reprex would be really helpful to give a specific suggestion.

If I understand correctly, you have or can create, say, a 3x1 matrix of years,highOZ

``````years <- c(1,2,3)
ozone <- c(0,1,1)
m <- as.matrix(years,ozone)
m
#>      [,1]
#> [1,]    1
#> [2,]    2
#> [3,]    3
mean(m)
#>  2
``````

Created on 2019-12-20 by the reprex package (v0.3.0)

The result is the mean number of years across one measuring station of ozone concentrations > 120ug/m3 (1 or TRUE). You can extend that of course to `n` stations.

Thanks!! How can I extend it? With a loop?

Well, again a `reprex` would get me closer, but I'll assume a data frame with colnames() = "station", "year1,", "year2" ... "year_n" where the year columns are logical or 1/0. Since you are only interested in the grand mean,

``````grand_mean <-  as.matrix[my_df[2:n])
mean(grand_mean)
``````
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.