averaging the 15 days before the day in common

Asked

Viewed 103 times

1

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

> 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

Tell you what, it’s not working out for me.

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 )
    1. dput(head(agua, 30)) and dput(head(meteo, 30)) would help (since we will need to do 15-day mobile media). 2) What is d? 3) What exactly doesn’t work?
  • d is a variable that contains the "Alqueva-Montante" station and averages the previous 15 days of the Dia of meteo

  • But her definition is not in the question code. Include this and also the dput(...) requested.

1 answer

1

your data has values NA in the date fields. To facilitate the response, I created two data.frame with the same names (water and Meteo) and only one column with values called x to realize the moving average of 15 days. Replace the x by 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])

Upshot:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.