Jorgen Harmse
2022-Dec-16 15:15 UTC
[R] Get data from a list of data frames (Stefano Sofia)
Following Bert Gunter's suggestion, I wonder why the data are in separate
frames (with hard-coded values) in the first place. You could put them in a text
file and call read.table. If you provide a header and put a meaningful station
name at the start of each data row then rownames of your data frame will be
meaningful.
Regards,
Jorgen Harmse.
From: R-help <r-help-bounces at r-project.org> on behalf of r-help-request
at r-project.org <r-help-request at r-project.org>
Date: Friday, 16December, 2022 at 05:00
To: r-help at r-project.org <r-help at r-project.org>
Subject: [EXTERNAL] R-help Digest, Vol 238, Issue 16
Message: 1
Date: Thu, 15 Dec 2022 13:52:35 +0000
From: Stefano Sofia <stefano.sofia at regione.marche.it>
To: "r-help at R-project.org" <r-help at R-project.org>
Subject: [R] Get data from a list of data frames
Message-ID: <fe9017cbd4ba49328e150061032068a7 at regione.marche.it>
Content-Type: text/plain; charset="utf-8"
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 at 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]]
------------------------------
Message: 4
Date: Thu, 15 Dec 2022 09:31:51 -0800
From: Bert Gunter <bgunter.4567 at gmail.com>
To: Gerrit Eichner <gerrit.eichner at math.uni-giessen.de>,
stefano.sofia at regione.marche.it
Cc: r-help at r-project.org
Subject: Re: [R] Get data from a list of data frames
Message-ID:
<CAGxFJbTgwQ2Nop0bjCzcT7Ri6FhDmOrXiXv+o2-2m_-sqZ6H_w at
mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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
[[alternative HTML version deleted]]
... and note that my previous version can be simplified using R's pipe
syntax to:
do.call('rbind',Total[select.stat]) |>
subset(sensor == 'thermometer', code)
## which calls the subset.data.frame() method that gives:
code
Station1.1 2583
Station4.4 4453
Station5.3 3179
close to the form that you suggested.
Same caveats as my previous post.
Cheers,
Bert
On Fri, Dec 16, 2022 at 7:16 AM Jorgen Harmse via R-help <
r-help at r-project.org> wrote:
> Following Bert Gunter's suggestion, I wonder why the data are in
separate
> frames (with hard-coded values) in the first place. You could put them in a
> text file and call read.table. If you provide a header and put a meaningful
> station name at the start of each data row then rownames of your data frame
> will be meaningful.
>
> Regards,
> Jorgen Harmse.
>
> From: R-help <r-help-bounces at r-project.org> on behalf of
> r-help-request at r-project.org <r-help-request at r-project.org>
> Date: Friday, 16December, 2022 at 05:00
> To: r-help at r-project.org <r-help at r-project.org>
> Subject: [EXTERNAL] R-help Digest, Vol 238, Issue 16
>
> Message: 1
> Date: Thu, 15 Dec 2022 13:52:35 +0000
> From: Stefano Sofia <stefano.sofia at regione.marche.it>
> To: "r-help at R-project.org" <r-help at R-project.org>
> Subject: [R] Get data from a list of data frames
> Message-ID: <fe9017cbd4ba49328e150061032068a7 at regione.marche.it>
> Content-Type: text/plain; charset="utf-8"
>
> 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 at 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]]
>
>
>
>
>
> ------------------------------
>
> Message: 4
> Date: Thu, 15 Dec 2022 09:31:51 -0800
> From: Bert Gunter <bgunter.4567 at gmail.com>
> To: Gerrit Eichner <gerrit.eichner at math.uni-giessen.de>,
> stefano.sofia at regione.marche.it
> Cc: r-help at r-project.org
> Subject: Re: [R] Get data from a list of data frames
> Message-ID:
> <
> CAGxFJbTgwQ2Nop0bjCzcT7Ri6FhDmOrXiXv+o2-2m_-sqZ6H_w at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> 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
>
>
>
> [[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.
>
[[alternative HTML version deleted]]