如何将R中两个不同长列的值配对?

时间:2016-06-29 09:26:38

标签: r

我有两个数据帧,格式与此相似:

     name1
1    Retta Mclennon
2      Deloras Wurm
3    Tristan Partee
4   Lashonda Swayne
5 Christiane Bendel

    name2
1  Christiane Bendel
2     Tristan Partee
3       Ronni Sugrue
4    Lashonda Swayne
5       Alvaro Fahey
6   Tamie Kalinowski
7   Jefferson Levitt
8     Retta Mclennon
9        Haywood Neu
10      Deloras Wurm

我想在name1中创建一个新列,其中值“Yes”将归于name2中包含的那些行。

1 个答案:

答案 0 :(得分:1)

            > df1
                           Name
            1    Retta Mclennon
            2      Deloras Wurm
            3    Tristan Partee
            4   Lashonda Swayne
            5 Christiane Bendel


         > df2
                            Name
            1  Christiane Bendel
            2     Tristan Partee
            3       Ronni Sugrue
            4    Lashonda Swayne
            5       Alvaro Fahey
            6   Tamie Kalinowski
            7   Jefferson Levitt
            8     Retta Mclennon
            9        Haywood Neu
            10      Deloras Wurm


    colnames(df1)[2] <- "Match"
    df1$Match <- data.frame(ifelse(df1$Name %in% df2$Name , "Yes", "No"))

> df1
               Name                                     Match
1    Retta Mclennon                                      Yes
2      Deloras Wurm                                      Yes
3    Tristan Partee                                      Yes
4   Lashonda Swayne                                      Yes
5 Christiane Bendel                                      Yes