0

what I wanted to do is to compare the first column of two data frame and find the indexes of the same value and assign the element of the second column of the first dataframe to the second dataframe : please see the example :

datafranmeA          dataframeB

   id   number         id   
1  1     45             1
2  3     78             4
3  5     67             12
4  12    18             5
5  4     44             8
6  8      32
7  13     41

output : dataframeB

         id     number
     1    1      45
     2    4      44
     3    12     18
     4    5      67
     5    8      32

I use a two for loop and if to compare but it is really slow as my own data is really big , how should I speed it up ?

for (i in 1:length(A[,1])){
 for (j in 1:length(B[,1])){
   if (A[i,1]==B[j,1]) {
      B[j,2]=A[i,2]}}}

thank you in advance,

3442
  • 8,248
  • 2
  • 19
  • 41
user3813088
  • 85
  • 1
  • 2
  • 7

1 Answers1

0

Try

library(dplyr)
left_join(dataframeB, dataframeA)
KFB
  • 3,501
  • 3
  • 15
  • 18