-3

I have two data. frame (suppose a,b having a size of 3172*1323, 3067*21), I want to merge both of them however, 21 columns are common in both and 3067 rows also common in both. I want to merge both data frame such that the common rows and common cols replaced and the final size will be 3172*1323 only I tried merge ( a,b, by =0) but it's not going to help me to get the data which I am looking for that. Please help me

A = read.table(header=TRUE, stringsAsFactors=FALSE, row.names=1, text='
Row.names   P1  P2  P3  P4  P5
R_1 1   2   3   4   5
R_2 6   7   8   9   10
R_3 8   6   4   2   1
R_4 2   4   6   8   10')

B = read.table(header=TRUE, stringsAsFactors=FALSE, row.names=1, text='
Row.names   P2  P5
R_2 NA  2
R_4 1   20')

# merge of A and B
C = read.table(header=TRUE, stringsAsFactors=FALSE, row.names=1, text='
Row.names   P1  P2  P3  P4  P5
R_1 1   2   3   4   5
R_2 6   NA  8   9   2
R_3 8   6   4   2   1
R_4 2   1   6   8   20')
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    did you try anything, any code or something? – surajs1n Jul 18 '18 at 07:06
  • 1
    Please read [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and edit your question. – pogibas Jul 18 '18 at 07:08
  • Please provide small reproducible example data, and expected output. – zx8754 Jul 18 '18 at 07:08
  • 1. search on this site: "[r] merge dataframes" 2. type `?merge` into R console. – Andre Elrico Jul 18 '18 at 07:12
  • You are looking for a simple join or merge if you will. When you have a large number of common columns you might want to try to find the ones that are duplicated problematically. Do the have the same labels? – Prometheus Jul 18 '18 at 07:13
  • I added the reproducible data and expected output – Manish Sharma Jul 18 '18 at 07:18
  • yes, I tried merge ( a,b, by = o) conisder 0 as row.names however the columns are increased ( 1323+21) but row.names also changed – Manish Sharma Jul 18 '18 at 07:20
  • Manish, please see the edit I've suggested for your code, one of a few ways demonstrated in the link that @PoGibas suggested you should read. I suggest you should put in a little more effort to make it clear what data you have and what code you've tried, in a way that is something we can just copy and paste into a console. And a hint on editing on SO: https://stackoverflow.com/editing-help – r2evans Jul 18 '18 at 07:22

2 Answers2

1

You don't need a merge. Using the data as I edited into your question:

A[row.names(B),colnames(B)] <- B
A
#     P1 P2 P3 P4 P5
# R_1  1  2  3  4  5
# R_2  6 NA  8  9  2
# R_3  8  6  4  2  1
# R_4  2  1  6  8 20

(You can use rownames or row.names, if you're curious.)

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • I tried but it's not giving the expected output because by using this I got data frame of 3067*21 and I want data frame size of 3067*1323 – Manish Sharma Jul 18 '18 at 08:35
  • @ManishSharma try looking at this code again ]. This is the right code. – Onyambu Jul 18 '18 at 08:45
  • Manish, if both `A` and `B` are simple frames, there is literally nothing about this that would change the dimensions of either. If your frame is changing shape then there is something else going on (perhaps it doesn't contain what you think it does). – r2evans Jul 18 '18 at 15:04
0

Thanks all, I tried the above method but it was not working in my case, however, I saw somewhere where they used the following approach 1. find the interaction rows or columns and remove it 2. After that just use cbind or rbind

  • This seems interesting and relevant, so it would be really useful for you to make this answer a little more reproducible. Suggestions: (1) include actual code (on your sample data) that demonstrates what you mean; and (2) include link to *"I saw somewhere"* if you have it. If you choose to not update the answer, I think it's more of a *comment* than an *answer*, since it lacks sufficient detail to be used by curious inexperienced programmers with similar problems. – r2evans Jul 26 '18 at 18:58