1

I've got a list of 10 with 2 columns for each element. One is the year (2001-2018) and the other the observation. For the first and second object are some years missing (so there lenght is only 14 or 16 instead of 18), the others are complete (length of 18).

My list:

list <- (A, B, C, D)

A = data.frame (Year = c(2001, 2003), Observ = c(1,3)
B = data.frame (Year = c(2002, 2003, 2018), Observ = c(2,8,3)
C = data.frame (Year = c(2001, 2002, 2003, ..., 2018), Observ = c(4,3,2,5)
D = data.frame (Year = c(2001, 2002, 2003, ..., 2018), Observ = c(17,1, 11, 2)

I would like to generate a dataframe looking like the following:

#Year Obser1 Obser2 Obser3 ... Obser10
#2001   1     NA     4           17
#2002   NA     2     3            1 
#2003   3      8     2            11
#...
#2018   NA     3     5            2
bwi321
  • 123
  • 6
  • 2
    Help us help you: Provide a [mcve]. In particular, please [edit] your question to include the output of the R command `dput(mydata)`, where you replace `mydata` with the name of the object you're talking about here. You can consult [How to make a great R reproducible example](https://stackoverflow.com/q/5963269/8386140) for more details. – duckmayr Jun 20 '20 at 11:33

1 Answers1

2

One way would be to merge the list of dataframes. If your list is called list_df you can do :

Reduce(function(x, y) merge(x, y, by = 'Year', all = TRUE), list_df)

#  Year Obser1 Obser2
#1 2000      1      1
#2 2001      2      2
#3 2002      3      3
#4 2003      4     NA

tidyverse alternative of the above

library(dplyr)
library(purrr)

list_df %>% reduce(full_join, by = 'Year')

data

Tested on this small sample :

df1 <- data.frame(Year = 2000:2003, Obser1 = 1:4)
df2 <- data.frame(Year = 2000:2002, Obser2 = 1:3)
list_df <- list(df1, df2)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213