[R] Get data from a list of data frames

Stefano Sofia @te|@no@@o||@ @end|ng |rom reg|one@m@rche@|t
Thu Dec 22 16:29:17 CET 2022


Sorry for the delay of my reply, I have been out of office for few days.

Thank you to all of you.

Both solutions work well; fortunately the structure of the data frames is identical, rbind works perfectly and this second solution is slightly easier to implement for me.


Thank you again for your help

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.sofia using regione.marche.it
---Oo---------oO----------------------------------------


________________________________
Da: Bert Gunter <bgunter.4567 using gmail.com>
Inviato: giovedì 15 dicembre 2022 18:31
A: Gerrit Eichner; Stefano Sofia
Cc: r-help using r-project.org
Oggetto: Re: [R] Get data from a list of data frames

Well, just for giggles, Gerrit's solution can be easily vectorized (i.e. no apply()-type stuff needed) IFF the structure of all the data frames are identical so that rbind() works:

d <-do.call('rbind',Total[select.stat]) ## one data frame to combine them all ;-)
d[d$sensor == 'thermometer','code']

Whether this is better, worse, or unneeded, I cannot say.

Cheers,
Bert


On Thu, Dec 15, 2022 at 6:11 AM Gerrit Eichner <gerrit.eichner using math.uni-giessen.de<mailto:gerrit.eichner using math.uni-giessen.de>> wrote:
Hello, Stefano,

maybe the following example contains what you want to achieve:

Station1 <- data.frame(sensor = c("thermometer", "raingauge",
"snowgauge", "anemometer"),
                        code = c(2583, 1478, 3178, NA))
Station2 <- data.frame(sensor = c("thermometer", "raingauge",
"snowgauge", "anemometer"),
                        code = c(2584, 1479, 3179, 4453))
Station3 <- data.frame(sensor = c("thermometer", "raingauge",
"snowgauge", "anemometer"),
                        code = c(2584, 1479, 3179, 4453))
Station4 <- data.frame(sensor = rev(c("thermometer", "raingauge",
"snowgauge", "anemometer")),
                        code = c(2584, 1479, 3179, 4453))
Station5 <- data.frame(sensor = c("raingauge", "snowgauge",
"thermometer", "anemometer"),
                        code = c(2584, 1479, 3179, 4453))

Total <- list("Station1"=Station1, "Station2"=Station2,
               Station3 = Station3, Station4 = Station4,
               Station5 = Station5)

select.stat <- paste0("Station", c(1, 4, 5))
select.sens <- "thermometer"

sapply(Total[select.stat], function(x, sens)
   x$code[x$sensor == sens], sens = select.sens)



  Hth --  Gerrit

---------------------------------------------------------------------
Dr. Gerrit Eichner                   Mathematical Institute, Room 212
gerrit.eichner using math.uni-giessen.de<mailto:gerrit.eichner using math.uni-giessen.de>   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
http://www.uni-giessen.de/eichner<https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.uni-giessen.de%2Feichner&e=5a635173&h=6288e95a&f=y&p=y>
---------------------------------------------------------------------

Am 15.12.2022 um 14:52 schrieb Stefano Sofia:
> Dear R-list users,
>
> I have a list of n data frames built as follows:
>
>
> Station1 <- data.frame(sensor = c("thermometer", "raingauge", "snowgauge", "anemometer"), code = c(2583, 1478, 3178, NA))
> Station2 <- data.frame(sensor = c("thermometer", "raingauge", "snowgauge", "anemometer"), code = c(2584, 1479, 3179, 4453))
> ....
>
> Total <- list("Station1"=Station1, "Station2"=Station2, ...)
>
> I would need to have a vector with the sensor codes of the thermometers of some stations, let's say of Station1, Station2 and Station5 (i.e. c(2583, 2584, 2587)).
> I tried with lapply, but I have not been able to get what I need.
> Could you please help me?
>
> Thank you
>
> 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.sofia using regione.marche.it<mailto:stefano.sofia using 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 using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help<https://urlsand.esvalabs.com/?u=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&e=5a635173&h=06ff70f3&f=y&p=y>
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&e=5a635173&h=e12f63e8&f=y&p=y>
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help<https://urlsand.esvalabs.com/?u=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&e=5a635173&h=06ff70f3&f=y&p=y>
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<https://urlsand.esvalabs.com/?u=http%3A%2F%2Fwww.R-project.org%2Fposting-guide.html&e=5a635173&h=e12f63e8&f=y&p=y>
and provide commented, minimal, self-contained, reproducible code.

--
Questo messaggio è stato analizzato con Libraesva ESG ed è risultato non infetto

________________________________

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