cptn
cptn

Reputation: 703

merge two data.frames and replace values of certain columns of df1 with values of df2

i have two data.frames that i want to merge and replace values of certain columns of df1 with values of df2. in this working example there are only 3 columns. but in the original data, there are about 20 columns that should remain in the final data.frame.

NO <- c(2, 4, 7, 18, 25, 36, 48)
WORD <- c("apple", "peach", "plum", "orange", "grape", "berry", "pear")
CLASS <- c("p", "x", "x", "n", "x", "p", "n")
ColA <- c("hot", "warm", "sunny", "rainy", "windy", "cloudy", "snow")
df1 <- data.frame(NO, WORD, CLASS, ColA)
df1

#   NO   WORD CLASS   ColA
# 1  2  apple     p    hot
# 2  4  peach     x   warm
# 3  7   plum     x  sunny
# 4 18 orange     n  rainy
# 5 25  grape     x  windy
# 6 36  berry     p cloudy
# 7 48   pear     n   snow

NO <- c(4, 18, 36)
WORD <- c("patricia", "oliver", "bob")
CLASS <- c("p", "n", "x")

df2 <- data.frame(NO, WORD, CLASS)
df2

#   NO     WORD CLASS
# 1  4 patricia     p
# 2 18   oliver     n
# 3 36      bob     x

i want to merge the two data.frames and replace the values of WORD and CLASS from df1 with the values of WORD and CLASS from df2

my data.frame should look like this:

#   NO      WORD CLASS   ColA
# 1  2     apple     p    hot
# 2  4  patricia     p   warm
# 3  7      plum     x  sunny
# 4 18    oliver     n  rainy
# 5 25     grape     x  windy
# 6 36       bob     x cloudy
# 7 48      pear     n   snow

Upvotes: 4

Views: 3944

Answers (2)

Tyler Rinker
Tyler Rinker

Reputation: 109874

This approach could work as well though is more playing around than the best answer to the question:

library(qdap); library(qdapTools)
df1[, 2] <- as.character(df1[, 2])
trms <- strsplit(df1[, 1] %lc% colpaste2df(df2, 2:3, keep.orig = FALSE), "\\.")
df1[sapply(trms, function(x) !all(is.na(x))), 2:3] <- 
    do.call(rbind, trms[sapply(trms, function(x) !all(is.na(x)))])

Upvotes: 0

Rufo
Rufo

Reputation: 534

Try this

auxind<-match(df2$NO, df1$NO)  # Stores the repeated rows in df1
dfuni<-(rbind(df1[,1:3],df2)[-auxind,])  # Merges both data.frames and erases the repeated rows from the first three colums of df1
dfuni<-dfuni[order(dfuni$NO),] # Sorts the new data.frame
df1[,1:3]<-dfuni 

Upvotes: 2

Related Questions