Sorry Rui; if you run your code you will get: Error in FUN(X[[i]], ...) : object 'ws' not found
Moreover, even if you did this: aggregate(wd ~ day + month, data=df, FUN = my_fun, ws1 = df$ws) the answer would be wrong is you need to include only the subsets of ws1 corresponding to the split defined by day + month in each call to my_fun. aggregate() does not do this (the ... argument is evaluated only once, not once per subset). So I think the answer to Stefano's question is no, aggregate doesn't work. You should use by() or tapply() instead: ##Warning: I haven't checked this carefully. So please do! > by(df, list(df$day,df$month), FUN = \(x)with(x, my_fun(wd, ws))) Cheers, Bert On Sat, May 13, 2023 at 8:54 AM Rui Barradas <[email protected]> wrote: > Às 15:51 de 13/05/2023, Stefano Sofia escreveu: > > 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: [email protected] > > ---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]] > > > > > > ______________________________________________ > > [email protected] 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. > Hello, > > Use the dots argument to pass any number of named arguments to your > aggregation function. > In this case, ws1 = ws at the end of the aggregate call. > > > aggregate(wd ~ day + month, data=df, FUN = my_fun, ws1 = ws) > > > You can also give the user the option to remove or not NA's by adding a > na.rm argument: > > > my_fun <- function(wd1, ws1, na.rm = FALSE) { > [...] > mean_u <- mean(u_component, na.rm = na.rm) > mean_v <- mean(v_component, na.rm = na.rm) > [...] > } > > aggregate(wd ~ day + month, data=df, FUN = my_fun, ws1 = ws, na.rm = TRUE) > > > Hope this helps, > > Rui Barradas > > ______________________________________________ > [email protected] 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]] ______________________________________________ [email protected] 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.

