'How to merge two data frames
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!
Solution 1:[1]
We can do a merge in base R
merge(df2, df1[c('b', 'd')], by.x = 'e', by.y = 'd')
Solution 2:[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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | akrun |
| Solution 2 | ThomasIsCoding |
