0

this seems to be pretty simple.

My goal is to just combine these two dataframes by row without using r_bind or merge since this would be dependent on my column names.

   obj <- data.frame (
      yr = c(1990, 1991, 1992, 1993, 1994, 1995),
      occ = c(223, 224, 225, 226, 227, 228))
    
    obj1 <- data.frame (
      emp = c(10, 10, 10, 10, 10, 10),
      degree = c(1, 1, 1, 1, 1, 1))
  

The expected output would be:

 emp degree
1  10      1
2  10      1
3  10      1
4  10      1
5  10      1
6  10      1
1 1990    223
2 1991    224
3 1992    225
4 1993    226
5 1994    227
6 1995    228

many thanks in advance!!

freddywit
  • 301
  • 1
  • 5
  • 2
    Replace the column names of the obj with obj1 and use `rbind` i.e. `rbind(obj1, setNames(obj, names(obj1)))` – akrun Apr 07 '21 at 21:09

1 Answers1

2

A base R option is list2DF + Map

> list2DF(Map(c, obj1, obj))
    emp degree
1    10      1
2    10      1
3    10      1
4    10      1
5    10      1
6    10      1
7  1990    223
8  1991    224
9  1992    225
10 1993    226
11 1994    227
12 1995    228

Also, you can try rbindlist from data.table package

data.table::rbindlist(list(obj1, obj), use.names = FALSE)

which gives

     emp degree
 1:   10      1
 2:   10      1
 3:   10      1
 4:   10      1
 5:   10      1
 6:   10      1
 7: 1990    223
 8: 1991    224
 9: 1992    225
10: 1993    226
11: 1994    227
12: 1995    228
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81