-1

I have 5 year data from 2017 to 2021 with pollutants pm2.5 , no2 , so2, and co. how can I find the yearly mean, standard deviation and median of each pollutants for each year?

library(openair)
pm2.5avg <- mean(newdata$pm25, year="2017", na.rm=TRUE)
Phil
  • 7,287
  • 3
  • 36
  • 66
Dia
  • 1
  • This is something that is easily handled by the `dplyr` [package](https://dplyr.tidyverse.org/reference/summarise.html). Probably something like `pm2.5vg <- newdata |> group_by(year) |> summarise(yearly_mean = mean(pm25), yearly_sd = sd(pm25), yearly_median = median(pm25))` is what you're after, but you'll have to ensure that your variable names are the same. Without seeing your data this is just a guess. Maybe edit your post and paste the result from calling `dput(newdata)` to show your data. See [here](https://stackoverflow.com/help/minimal-reproducible-example) why. – Godrim Jan 25 '23 at 07:15
  • Does this answer your question? [Calculate group mean, sum, or other summary stats. and assign column to original data](https://stackoverflow.com/questions/6053620/calculate-group-mean-sum-or-other-summary-stats-and-assign-column-to-original) – Limey Jan 25 '23 at 08:13
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 25 '23 at 09:25

1 Answers1

0
library(tidyverse)
library(lubridate)

newdata <- openair::mydata

newdata %>%
  filter(between(year(date), 2001, 2005)) %>%
  group_by(year = year(date)) %>%
  summarise(across(pm25, list(
    mean = mean, sd = sd, median = median
  ), na.rm = TRUE))


#> # A tibble: 5 × 4
#>    year pm25_mean pm25_sd pm25_median
#>   <dbl>     <dbl>   <dbl>       <dbl>
#> 1  2001      25.0   17.0           22
#> 2  2002      21.4    9.94          20
#> 3  2003      19.1   10.4           17
#> 4  2004      19.3    9.36          18
#> 5  2005      18.0    9.79          16

Created on 2023-01-25 with reprex v2.0.2

Chamkrai
  • 5,912
  • 1
  • 4
  • 14