1

So let's say that I have two data frames. So for example:

a <- c(10,20,30,40)
b <- c('b', 'p', 't', 'x')
c <- c(TRUE,FALSE,TRUE,FALSE)
d <- c(2.5, 8, 10, 7)
df1 <- data.frame(a,b,c,d)


e<-c(2.5,2.5,8,8,8,10,10,10)
f<-c(T, T, F, F, F, T, F, T)
df2<- data.frame(e,f)

I know that all the values of column e in dataframe 2 are contained in column d of dataframe 1.

I want to be able to place column b into dataframe 2 so that it would look like this:

e<-c(2.5,2.5,8,8,8,10,10,10)
f<-c(T, T, F, F, F, T, F, T)
b<-b("b", "b", "p", "p", "p", "t", "t", "t")
df2<- data.frame(e,f,c)

That is, where a value in column e in dataframe 2 is equal to a value in column d of dataframe 1, I want to place the value of column C corresponding to that value in column D into a new column in Dataframe 2.

In reality, I am using much larger datasets than this, so I am hoping for something that does this in a timely manner(i.e preferably not nested for loops). Any help would be greatly appreciated!

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
tfr950
  • 402
  • 2
  • 8
  • 1
    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) – massisenergy Apr 01 '20 at 19:45

2 Answers2

3

We can do a merge in base R

merge(df2, df1[c('b', 'd')], by.x = 'e', by.y = 'd')
akrun
  • 874,273
  • 37
  • 540
  • 662
2

Another base R solution using match

df2$b <- df1$b[match(df2$e,df1$d)]

which gives

> df2
     e     f b
1  2.5  TRUE b
2  2.5  TRUE b
3  8.0 FALSE p
4  8.0 FALSE p
5  8.0 FALSE p
6 10.0  TRUE t
7 10.0 FALSE t
8 10.0  TRUE t
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81