合并两个data.frames并将df1的某些列的值替换为df2的值

时间:2014-03-09 15:10:16

标签: r replace merge tm

我有两个data.frames,我想合并并替换df1的某些列的值 值为df2。在这个工作示例中,只有3列。但在原始数据中, 大约有20列应保留在最终的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

我想合并两个data.frames并从df1替换WORD和CLASS的值 使用来自df2的WORD和CLASS的值

我的data.frame应如下所示:

#   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

2 个答案:

答案 0 :(得分:2)

试试这个

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 

答案 1 :(得分:0)

这种方法可以起作用,但是比这个问题的最佳答案更能解决问题:

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)))])