[R] aggregate wind direction data with wind speed required

Stefano Sofia @te|@no@@o||@ @end|ng |rom reg|one@m@rche@|t
Sat May 13 16:51:27 CEST 2023

Dear list users,

I have to aggregate wind direction data (wd) using a function that requires also a second input variable, wind speed (ws).

This is the function that I need to use:

my_fun <- function(wd1, ws1){

  u_component <- -ws1*sin(2*pi*wd1/360)
  v_component <- -ws1*cos(2*pi*wd1/360)
  mean_u <- mean(u_component, na.rm=T)
  mean_v <- mean(v_component, na.rm=T)
  mean_wd <- (atan2(mean_u, mean_v) * 360/2/pi) + 180
  result <- mean_wd

Does the aggregate function work only with functions with a single input variable (the one that I want to aggregate), or its use can be extended to functions with two input variables?

Here a simple example (which is meaningless, the important think is the concept behind it):
df <- data.frame(day=c(1, 1, 1, 2, 2, 2, 3, 3), month=c(1, 1, 2, 2, 2, 2, 2, 2), wd=c(45, 90, 90, 135, 180, 270, 270, 315), ws=c(7, 7, 8, 3, 2, 7, 14, 13))

aggregate(wd ~ day + month, data=df, FUN = my_fun)

cannot work, because ws is not taken into consideration.

I got lost. Any hint, any help?
I hope to have been able to explain my problem.
Thank you for your attention,

--oOO--( )--OOo--------------------------------------
Stefano Sofia PhD
Civil Protection - Marche Region - Italy
Meteo Section
Snow Section
Via del Colle Ameno 5
60126 Torrette di Ancona, Ancona (AN)
Uff: +39 071 806 7743
E-mail: stefano.sofia using regione.marche.it


AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu� contenere informazioni confidenziali, pertanto � destinato solo a persone autorizzate alla ricezione. I messaggi di posta elettronica per i client di Regione Marche possono contenere informazioni confidenziali e con privilegi legali. Se non si � il destinatario specificato, non leggere, copiare, inoltrare o archiviare questo messaggio. Se si � ricevuto questo messaggio per errore, inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio computer. Ai sensi dell'art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessit� ed urgenza, la risposta al presente messaggio di posta elettronica pu� essere visionata da persone estranee al destinatario.
IMPORTANT NOTICE: This e-mail message is intended to be received only by persons entitled to receive the confidential information it may contain. E-mail messages to clients of Regione Marche may contain information that is confidential and legally privileged. Please do not read, copy, forward, or store this message unless you are an intended recipient of it. If you have received this message in error, please forward it to the sender and delete it completely from your computer system.

Questo messaggio  stato analizzato da Libraesva ESG ed  risultato non infetto.
This message was scanned by Libraesva ESG and is believed to be clean.

	[[alternative HTML version deleted]]

More information about the R-help mailing list