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!

1

2 Answers 2

2

We can do a merge in base R

merge(df2, df1[c('b', 'd')], by.x = 'e', by.y = 'd')
Sign up to request clarification or add additional context in comments.

2 Comments

nice to see merge solution, + 1
Thank you for the quick reply! both solutions work like a charm and are a lot quicker than what I was doing:)
1

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

1 Comment

Thank you for the quick reply! This worked like a charm.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.