使用匹配的列行名称/值替换data.frame内容与另一个表中的值

时间:2018-03-10 20:26:58

标签: r

我的两个(输入)data.frames看起来像这样:

df1

 Group Name      Vote
    AB    a Sentence1
    AB    b Sentence2
    AB    c Sentence3
    XY    d Sentence4
    XY    e Sentence5
    XY    f Sentence6
    ZW    a Sentence7
    ZW    b Sentence8
    ZW    c Sentence9

df2

 AB XY ZW
  a  d  a
  b  e  b
  c  f  c

我需要用 df1 $ Vote 替换 df2 各自的内容。请注意,AB组和ZW组共享相同的Name元素 - 因此同时检查两个(列)名称和表之间的内容匹配。

像这样:

Resulting df3:

        AB        XY        ZW
 Sentence1 Sentence4 Sentence7
 Sentence2 Sentence5 Sentence8
 Sentence3 Sentence6 Sentence9

非常感谢&感谢任何帮助!

df1&的代码DF2:

df1 <- data.frame(Group = c("AB", "AB", "AB", "XY", "XY", "XY", "ZW", "ZW", "ZW"), 
                  Name = c("a", "b", "c", "d", "e", "f", "a", "b", "c"), 
                  Vote = c("Sentence1", "Sentence2", "Sentence3", "Sentence4", "Sentence5", "Sentence6", "Sentence7", "Sentence8", "Sentence9"))

df2 <- data.frame(AB = c("a", "b", "c"), 
                  XY = c("d", "e", "f"), 
                  ZW = c("a", "b", "c"))

1 个答案:

答案 0 :(得分:0)

以下是reinterpret_cast方法:

dplyr