Reputation: 59
I have two dataframes of the following form
df1:
first | second |
---|---|
1 | 5 |
2 | 6 |
3 | 7 |
df2:
sec | fir |
---|---|
3 | 4 |
2 | 1 |
1 | 3 |
I'm trying to use rbind
(or anything else that works haha) to merge the rows of these two dataframes by using a partial match on the column names (matching first
with fir
, and second
with sec
) to create a dataframe that looks like this
first | second |
---|---|
1 | 5 |
2 | 6 |
3 | 7 |
4 | 3 |
1 | 2 |
3 | 1 |
Thank you all in advance for the help!!!!
Upvotes: 0
Views: 208
Reputation: 887048
With rbind
, it needs the columns to have the same name. So, the option is to set the names of the second data with the names of the first one and then rbind
rbind(df1, setNames(df2, names(df1)))
If we need to automate by checking the substring, a distance based approach can be used
library(phonics)
nm1 <- c(names(df1), names(df2))
ind <- soundex(nm1, maxCodeLen = 2)
nm2 <- ave(nm1, ind, FUN = function(x) x[1])
rbind(df1, setNames(df2[match(ind[1:2], ind[3:4])], names(df1)))
Upvotes: 2
Reputation: 388907
You can use grep
to find the column name in df2
that is similar to one in df1
.
cols <- sapply(names(df2), function(x) grep(x, names(df1)))
cols
#sec fir
# 2 1
Using cols
you can rearrange df2
, make their names similar to df1
and then rbind
.
rbind(df1, setNames(df2[cols], names(df1)))
# first second
#1 1 5
#2 2 6
#3 3 7
#4 4 3
#5 1 2
#6 3 1
Upvotes: 1