Question: (Closed) R join data frame based on the value of a column
1
Vincent Manzanilla • 30 wrote:
Hi,
I would like to keep the line where the ID are the same. I know how to to it in bash nut I would like to succeed with R. I have two files with 500,000 lines and 300,000 lines.
df1 <- data.frame(ID=c(sample(LETTERS,20,replace=FALSE)),A=sample(1:100,20,replace=FALSE),B=sample(100:1000,20,replace=FALSE))
ID A B
1 F 81 337
2 J 14 400
3 B 1 620
4 L 40 326
5 T 86 315
6 D 94 797
7 U 96 309
8 Y 42 939
9 Z 18 397
10 W 8 168
11 R 71 308
12 I 83 591
13 O 67 462
14 C 63 353
15 S 88 362
16 Q 13 917
17 G 58 289
18 P 55 640
19 E 9 218
20 X 59 920
and
df2 <- data.frame(D=sample(1:100,10,replace=FALSE),E=sample(100:1000,10,replace=FALSE), ID=c(sample(LETTERS,10,replace=FALSE)))
D E ID
1 29 551 U
2 33 191 C
3 85 814 Q
4 55 897 D
5 46 527 V
6 57 603 T
7 45 287 K
8 79 409 Z
9 65 243 W
10 90 701 M
I would like to join the two data frames where the column ID are similar.
ID A B D E
T 86 315 57 603
D 94 797 55 897
U 96 309 29 551
Z 18 397 79 409
W 8 168 65 243
C 63 353 33 191
Q 13 917 85 814
Hello Vincent Manzanilla!
We believe that this post does not fit the main topic of this site.
This is basic R that can be googled, e.g. https://stackoverflow.com/questions/41472722/how-to-merge-two-data-frames-based-on-similar-values-in-r
For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.
If you disagree please tell us why in a reply below, we'll be happy to talk about it.
Cheers!
Thank you for the link!
The code for my problem was the following:
More concise than the code suggested.