average of the 15 days before the day in common

1

Hello! I'm trying to find in the database ( agua and meteo ) the day of the month of agua in common with meteo and when I find meteo it finds the same day (for Alqueva-Amante station) , make it the average of the 15 days before the day in common.

> head(agua)
   Ano Mes Dia          Estacao Secchi    OD Turb Temp Ts_Tf SST Clorofila_a
1 2002   1  NA Alqueva-Montante     NA    NA   NA   NA    NA  NA          NA
2 2002   1  NA   Alqueva-Mourão     NA    NA   NA   NA    NA  NA          NA
3 2002   2  14 Alqueva-Montante     NA 22.03   NA   NA    NA  18       186.0
4 2002   2  21 Alqueva-Montante     NA 11.39   NA   NA    NA  12        31.5

> head(meteo)
   Ano Mes Dia          Estacao DirVento HumidadeRelativa Precipitacao   Pressao Radiacao
1 2002   7  19 Alqueva-Montante 347.4361         39.27273           NA  999.7417 373.3333
2 2002   7  20 Alqueva-Montante 339.2638         57.91667           NA 1003.0917 215.7917
3 2002   7  21 Alqueva-Montante 337.9830         59.54167           NA 1003.7792 308.0833
4 2002   7  22 Alqueva-Montante 343.3900         68.58333           NA 1007.1625 169.2917
5 2002   7  23 Alqueva-Montante 324.4686         57.04167           NA 1008.2417

I did the following, it's not working.

library(dplyr)
d %>%
  group_by(meteo$Estacao=='Alqueva-Montante') %>%
  summarise_at(meteo$Dia[-15], mean, na.rm)

a <- ifelse(meteo$Dia=agua$Dia, d, agua$Dia )
    
asked by anonymous 19.03.2018 / 19:38

1 answer

1

Your data has NA values in the date fields. To facilitate the answer, I created two data.frame with the same names (water and meteo) and only one column with values called x to make the 15-day moving average. Replace x with any of the variables you have to find the desired result.

require(lubridate)
require(zoo)

set.seed(200)

#Cria data.frame agua e data.frame meteo 
agua = data.frame(data=seq(ymd("2018-01-01"),ymd("2018-03-01"),by=1))
agua$x = rnorm(nrow(agua))
meteo = data.frame(data=seq(ymd("2018-02-01"),ymd("2018-03-01"),by=1))
meteo$x = rnorm(nrow(meteo))

#Calcula a média móvel com janela de 15 dias para todos os registros de água
media.movel=rollmean(agua$x,15,fill=NA,align="right")

#Encontra posições em que as datas de água são as mesmas de meteo
datas.comum = which(agua$data %in% meteo$data)

#Seleciona as médias móveis em que as datas também aparecem em meteo 
resultado =data.frame( data=agua$data[datas.comum],x=media.movel[datas.comum])

Result:

data            x
1  2018-02-01  0.055510521
2  2018-02-02  0.009890840
3  2018-02-03  0.019412893
4  2018-02-04 -0.067093997
5  2018-02-05 -0.106476535
6  2018-02-06 -0.145495980
7  2018-02-07 -0.274031171
8  2018-02-08 -0.192604522
9  2018-02-09 -0.194224005
10 2018-02-10 -0.034190309
11 2018-02-11 -0.090353161
12 2018-02-12 -0.102533618
13 2018-02-13 -0.040032215
14 2018-02-14 -0.072145853
15 2018-02-15 -0.006585583
16 2018-02-16 -0.177623894
17 2018-02-17 -0.209649552
18 2018-02-18 -0.191372715
19 2018-02-19 -0.112505032
20 2018-02-20 -0.055332672
21 2018-02-21 -0.130381239
22 2018-02-22 -0.171130422
23 2018-02-23 -0.162399432
24 2018-02-24 -0.186730411
25 2018-02-25 -0.159728167
26 2018-02-26 -0.249345152
27 2018-02-27 -0.240565129
28 2018-02-28 -0.153800058
29 2018-03-01 -0.138120627
    
19.03.2018 / 23:22