合并/组合和匹配R中具有不等行数的数据帧

时间:2017-04-08 21:11:13

标签: r dataframe merge match cbind

我们说我有两个data.frames df1df2

> df1
    Var1   Var2
1    "A"    "D"
2    "B"    "E"
3    "C"    "F"

> df2
    Var3   Var4
1    "C"    "H"
2    "B"    "I"
3    "G"    "J"
4    "A"    "K"

我希望将df1df2合并/合并,使得Var1Var3的值尽可能匹配,否则NAVar1重要的是,我希望保留Var2> df2 Var3 Var4 Var1 Var2 1 "C" "H" "C" "F" 2 "B" "I" "B" "E" 3 "G" "J" NA NA 4 "A" "K" "A" "D" 的值之间的映射。在这个简单的例子中,我最终得到:

    $sql = $db->query('SELECT c.comment, c.date_comment 
                                      FROM comments as c 
                                      INNER JOIN joint_a_comments on joint_a_comments.id_comment = c.id
                                      INNER JOIN articles a ON joint_a_comments.id_article = a.id
                                      WHERE a.id ="'.$idarticle.'"
                                      ORDER BY c.date_comment DESC 
                                      LIMIT 0,10');

    while($result[] = $sql->fetch(PDO::FETCH_ASSOC))
    {
        $rescomment[] = new Comments($result);
    }

    return $rescomment;

关于实现这一目标的一般方法的任何想法?每个data.frame中的变量数实际上不一定与本例中的相同。干杯! :)

1 个答案:

答案 0 :(得分:0)

使用$entireForm .= $this->addInputError_New_Element($stage_option);

match