[R] apply a function to a list of data frames

Stefano Sofia stefano.sofia at regione.marche.it
Tue May 26 14:47:23 CEST 2015


Thank you for your help.
Your explanations have been very useful.

Stefano

________________________________________
Da: Rui Barradas [ruipbarradas at sapo.pt]
Inviato: venerdì 22 maggio 2015 20.26
A: Stefano Sofia; r-help at r-project.org
Oggetto: Re: [R] apply a function to a list of data frames

Hello,

You should change your function to accept only one argument, the
data.frames, and then use lapply (not sapply).
Something like the following.

calc <- function(dat)
{
        bias_dmo_max <- round(mean((dat$dmo_12-dat$Eonestep_12), na.rm=TRUE),
digits=2)
        rmse_dmo_max <- round(sqrt(mean((dat$dmo_12-dat$Eonestep_12)^2,
na.rm=TRUE)), digits=2)
        bias_dmo_min <- round(mean((dat$dmo_27-dat$Eonestep_27), na.rm=TRUE),
digits=2)
        rmse_dmo_min <- round(sqrt(mean((dat$dmo_27-dat$Eonestep_27)^2,
na.rm=TRUE)), digits=2)
        result <- list(bias_dmo_max, rmse_dmo_max, bias_dmo_min, rmse_dmo_min)
        result
}

result_lst <- lapply(lst, calc)


Hope this helps,

Rui Barradas


Em 22-05-2015 19:02, Stefano Sofia escreveu:
> Dear R-users,
> given a list of dataframes (like below reported), for each month I need to apply a function (called calc).
> The result should be written in a new list of data frames, one row for each month.
>
> I have been trying to use sapply, with no success.
> Could somebody help me in this?
>
>
> $df1
> day dmo_12 Eonestep_12 tmax dmo_27 Eonestep_27 tmin
> 2012-12-01 11 13 13 9 8 8
> 2012-12-02 11 13 13 5 6 8
> 2012-12-03 6 10 8 6 6 7
> 2012-12-04 11 13 9 6 6 3
> 2012-12-05 8 10 12 5 6 7
> 2012-12-06 8 10 8 4 5 4
> 2012-12-07 7 9 8 6 6 5
> ...
>
>
> calc <- function(dmo_12, Eonestep_12, dmo_27, Eonestep_27)
> {
> bias_dmo_max <- round(mean((dmo_12-Eonestep_12), na.rm=TRUE), digits=2)
> rmse_dmo_max <- round(sqrt(mean((dmo_12-Eonestep_12)^2, na.rm=TRUE)), digits=2)
> bias_dmo_min <- round(mean((dmo_27-Eonestep_27), na.rm=TRUE), digits=2)
> rmse_dmo_min <- round(sqrt(mean((dmo_27-Eonestep_27)^2, na.rm=TRUE)), digits=2)
> result <- list(bias_dmo_max, rmse_dmo_max, bias_dmo_min, rmse_dmo_min)
> result
> }
>
>
> Thank you for your help
> Stefano
>
>
> ________________________________
>
> 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.
>
>       [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>

________________________________

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.



More information about the R-help mailing list