You don't have to bother with the subtracting from pi/2 bit ... just assume the cartesian complex values are (y,x) instead of (x,y).
On May 13, 2023 1:38:51 PM PDT, Bill Dunlap <williamwdun...@gmail.com> wrote: >I think that using complex numbers to represent the wind velocity makes >this simpler. You would need to write some simple conversion functions >since wind directions are typically measured clockwise from north and the >argument of a complex number is measured counterclockwise from east. E.g., > >windToComplex <- >function(speed, degreesCW) { > complex(mod=speed, arg=(90-degreesCW)/180*pi) >} >complexToWind <- >function(z) { > # Convert complex velocity z to speed and direction (degrees clockwise > # from north, in range [0,360)). > stopifnot(is.complex(z)) > data.frame(speed = Mod(z), degreesCW = (pi - Arg(z*1i))/(2*pi)*360) >} > >Then use FUN=mean instead of my_fun. > >-Bill > >On Sat, May 13, 2023 at 7:51 AM Stefano Sofia < >stefano.so...@regione.marche.it> wrote: > >> 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 >> result >> } >> >> 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, >> Stefano >> >> >> (oo) >> --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.so...@regione.marche.it >> ---Oo---------oO---------------------------------------- >> >> ________________________________ >> >> 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]] >> >> ______________________________________________ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > [[alternative HTML version deleted]] > >______________________________________________ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. -- Sent from my phone. Please excuse my brevity. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.