I have two lists: one list contains a dataframe for each province of the country, indicating the number of patients by age group:
GrupoEdad <- c("0-20","21-65","65 y más")
Enfermos <- c(15,52,65)
Provincia1 <- data.frame(GrupoEdad,Enfermos)
GrupoEdad2 <- c("0-20","21-65","65 y más")
Enfermos2 <- c(8,38,55)
Provincia2 <- data.frame(GrupoEdad2,Enfermos2) %>%
rename("GrupoEdad" = "GrupoEdad2", "Enfermos" = "Enfermos2")
lista1 <- list(Provincia1,Provincia2)
The other contains a dataframe for each province of the country, indicating the population by age group:
GrupoEdad <- c("0-20","21-65","65 y más")
Poblacion <- c(11000,40000,5600)
Provincia1 <- data.frame(GrupoEdad,Poblacion)
GrupoEdad2 <- c("0-20","21-65","65 y más")
Poblacion2 <- c(5400,29000,4000)
Provincia2 <- data.frame(GrupoEdad2,Poblacion2) %>%
rename("GrupoEdad" = "GrupoEdad2", "Poblacion" = "Poblacion2")
lista2 <- list(Provincia1,Provincia2)
All dataframes have a common variable: “GrupoAge”.
Is there any way to make a left_join between the dataframes in each list corresponding to each province? The idea is that, for each province, obtain a dataframe with both the number of patients and the population by age group, in order to calculate rates.
The situation to which I want to apply this concept is a bit more complex, with more dataframes of larger size, but basically this is what I would have to solve.
The first problem is that you have
data.frames
to have lost the province name information, so this would be the first thing that would work. The simplest way could be to give each list a name, for example:Another way would be that the data already reaches you as a column.
Once this is done, you can use
tidyverse
and several of its functions to:data.frame
into a unique one and transform the name of the list into a columnNote:
.id = 'provincia'
it is used to capture the data of the province from the name of the list, if this data already arrives as a column, do not use this parameter.