-1

I wish to combine multiple data frames (17 total) that all have a column with yyyy-mm, and other columns with corresponding values for that month.

The data frames do not all start at the same month, they also do not have similar lengths.

I wish to combine them so that each represented yyyy-mm in all my data frames is represented in the combined data frame, and fill rows when no value was found with NA, so nothing is skipped.

How do I do this?

lmo
  • 37,904
  • 9
  • 56
  • 69
  • 3
    Please provide a minimal, [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with a the expected output. – lmo Nov 21 '17 at 12:58
  • You want to look into the merge function: you want to merge all your data frame on the common column (your date) – denis Nov 21 '17 at 13:11

2 Answers2

1

Here is tidyverse with full_join but reproduciple examples are better to understand your problem. With multiple data.frames just use pipes.

tbl_a <- tibble(
  month = c("2017-01", "2017-02", "2017-03"),
  value = c(1, 2, 3)
)

tbl_b <- tibble(
  month = c("2017-01", "2017-03", "2017-04"),
  value = c(5, 6, 7)
)

tbl_c <- tibble(
  month = c("2017-01", "2017-06", "2017-07"),
  value = c(8, 9, 10)
)

full_join(tbl_a, tbl_b, by="month") %>% 
  full_join(tbl_c, by="month")

# A tibble: 6 x 4
    month value.x value.y value
    <chr>   <dbl>   <dbl> <dbl>
1 2017-01       1       5     8
2 2017-02       2      NA    NA
3 2017-03       3       6    NA
4 2017-04      NA       7    NA
5 2017-06      NA      NA     9
6 2017-07      NA      NA    10
Hakki
  • 1,440
  • 12
  • 26
0

Thank you guys for responding. Fixed it using: reg_month_temp <- merge(ben_regsum, delta_regsum, by = "year_month", all = TRUE)

Then added one data frame at a time to the merged data frame.

This worked well; by using all = TRUE it kept every entry for year_month and filled the rest with NA.