0

(I come from a SAS background where one can set an index column.)

Suppose I have the following code:

a <- 11:20  
b <- 101:110  
c <- 131:140  
abc <-data.frame(a, b, c)  
abc 

I expect to get the following:

     a   b     c  
1    11  101   131  
2    12  102   132  
3    13  103   133  
4    14  104   134  
5    15  105   135  
6    16  106   136  
7    17  107   137  
8    18  108   138  
9    19  109   139  
10   20  110   140  

Suppose that I run a function from some analytical package that gives me back a column "a" along with a
new column "d" but deletes two of the rows. The package keeps the original row number, even though two
rows were omitted. Let's call the new data frame xyz.

     a     d  
1    11    3  
2    12   32    
3    13    2    
4    14    5  
5    15    8  
7    17   21  
8    18    1  
10   20   13  

Now I want to merge abc with xyz based on the 8 index value, so that I get this:

     a    b    c     d  
1    11  101   131   3  
2    12  102   132  32  
3    13  103   133   2  
4    14  104   134   5  
5    15  105   135   8  
7    17  107   137  21  
8    18  108   138   1  
10   20  110   140  13  

How can that be accomplished?

Thanks!

Andrew

markus
  • 25,843
  • 5
  • 39
  • 58
  • You have a typo. Variable `b` has 20 values. Being thrifty, R recycles variables `a` and `c` so that `abc` has 20 rows! The answer to the question is to use `merge` which gives the option of retaining or discarding unmatched rows. – dcarlson Jul 29 '20 at 20:58
  • 2
    Does this answer your question? [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – RyanFrost Jul 29 '20 at 20:59
  • I appreciate your sharing that link. But I'm fine on merging. My problem is that I want to merge on the index or id column, the one that starts out with row numbers. – mercator88 Jul 30 '20 at 12:29

1 Answers1

0

Here is base R option with merge + row.names, e.g.,

> `row.names<-`(merge(abc,xyz,by = "a",all.y = TRUE),row.names(xyz))
    a   b   c  d
1  11 101 131  3
2  12 102 132 32
3  13 103 133  2
4  14 104 134  5
5  15 105 135  8
7  17 107 137 21
8  18 108 138  1
10 20 110 140 13
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81