[R] lapply on list of lists

Stefano Sofia stefano.sofia at regione.marche.it
Fri Feb 12 18:03:49 CET 2016


Dear R list users,
I have three lists of data frames, respectively temp_list, wind_list and snow_list.
The elements of these three lists are

temp_list$station1, temp_list$station2 and temp_list$station3 with columns date and temp;
wind_list$station1, wind_list$station2 and wind_list$station3 with columns date, wind_vel and wind_dir;
snow_list$station1, snow_list$station2 and snow_list$station3 with columns date and hs

where date has been transformed to character.
I need to merge temp_list$station1, wind_list$station1 and snow_list$station1, and same thing for station2 and station3.

If I create a list
list_all <- list(temp_list$station1, wind_list$station1, snow_list$station1)

then
Reduce(function(x, y) merge(x, y, by=c("date"), all=TRUE), list_all)

will do it. But then I have to create the other two lists and apply again Reduce.

I would like to create a list of list and using lapply twice in order to get this process completely automatic.
I tried

list_all <- list(temp_list, wind_list, snow_list)
names(list_all) <- c("temp", "wind", "snow")
lapply(names(list_all), function(val){list_all$val, lapply(c("station1", "station2", "station3"), function(val){Reduce(function(x, y) merge(x$val, y$val, by=c("date"), all=TRUE), list_all)}), list_all})

but it gives me a syntax error and I am struggling to make it work.
Could someboby help me to create the correct loop?

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]]



More information about the R-help mailing list