0

I have two datasets that need to be stacked on top of each-other. Think of them as two subsets of one data-set. The issue is that they have completely different variables other than the "record_id" and one more variable

ds_app <- data.frame(record_id, a, b, c, d, e, f)
ds_vo  <- data.frame(record_id, g, h, i, j, k, l , m, n, o, p, q)

Is there an easy way to stack these other than having to create dummy variables; variables with assigned NA values.

Thanks so much!

  • 1
    You're probably looking for `?merge`, not [reproducible](https://stackoverflow.com/a/5963610/6574038) though. – jay.sf Oct 10 '20 at 07:43

2 Answers2

0

I guess you may need merge

merge(df1,df2, all = TRUE)

Example

> df1 <- data.frame(id = 1:3, a = 1:3, b = 4:6)

> df2 <- data.frame(id = 1:5, g = 1:5, h = 6:10, i = 11:15)

> df1
  id a b
1  1 1 4
2  2 2 5
3  3 3 6

> df2
  id g  h  i
1  1 1  6 11
2  2 2  7 12
3  3 3  8 13
4  4 4  9 14
5  5 5 10 15

> merge(df1,df2, all = TRUE)
  id  a  b g  h  i
1  1  1  4 1  6 11
2  2  2  5 2  7 12
3  3  3  6 3  8 13
4  4 NA NA 4  9 14
5  5 NA NA 5 10 15
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
0

Another option with full_join

library(dplyr)
full_join(df1, df2)

data

df1 <- data.frame(id = 1:3, a = 1:3, b = 4:6)
 df2 <- data.frame(id = 1:5, g = 1:5, h = 6:10, i = 11:15)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you ```akarun``` and ```ThomasIsCoding```. I ended up doing ```dplyr: : union_all```. :) – Maleeha Shahid Oct 15 '20 at 02:35
  • Also, I created missing variables in both datasets and assigned them a NA value to make sure order of rows and/or columns created was harmonized when stacking. – Maleeha Shahid Oct 15 '20 at 02:37