[R] create a vector from several data frames

Stefano Sofia stefano.sofia at regione.marche.it
Mon May 18 12:49:16 CEST 2015


Thank you David.
It is exactly what I was looking for and it works pefectly.

Stefano
________________________________________
Da: David L Carlson [dcarlson at tamu.edu]
Inviato: giovedì 14 maggio 2015 17.18
A: Stefano Sofia; r-help at r-project.org
Oggetto: RE: create a vector from several data frames

If you combine all of the df's into a list, e.g.

dfn <- paste0("df", 1:20)
df <- lapply(dfn, get)
names(df) <- dfn

and if "target" is the day you want in the same date/time format as the day variable in the data frames:

sapply(df, function(x) x[x$day==target, "tmax"])

will return a named vector of the tmax values.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Stefano Sofia
Sent: Thursday, May 14, 2015 9:23 AM
To: r-help at r-project.org
Subject: [R] create a vector from several data frames

Dear r-users,
suppose that I have 20 data frames df1, df2, ..., df20 (one for each different location) with the same column names and column types (the first column contains a date, the others are numeric) like

day tmax tmin
2015-05-10 20 10
2015-05-11 21 12
2015-05-12 17 9
2015-05-13 24 13
2015-05-14 25 18

I need to create a vector "tmax_all" of length 20 with the tmax referred to a particular day (let's say 2015-05-14).
I would first build a new data frame

tmax_df <- Reduce(function(x, y) merge(x, y, by="day"), list(df1[ , c("day", "tmax")], df2[ , c("day", "tmax")], ..., df20[ , c("day", "tmax")]))

and then select the row of tmax_df where day is the day I want to.
Is there an easiest way? Is it possible to create straightforward this vector without passing through the merge of all the data frames?

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.
______________________________________________
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