匹配两列不同的数据帧

时间:2016-05-19 06:02:36

标签: r merge

如何使用R匹配不同数据帧的两列?匹配值应在同一位置对齐,缺失值为NA。

输入:

df1$A: w x y 
df2$B: x y z 

预期输出数据框:

w  x y NA
NA x y z 

2 个答案:

答案 0 :(得分:1)

应该有一种更好的方式来代表这一点,但现在我可以解决这个问题。正如@akrun的评论中所提到的,您可以使用unique(rbind(cbind(as.character(df1$A), as.character(df2$B[match(df1$A, df2$B)])), cbind(as.character(df2$B), as.character(df1$A[match(df2$B, df1$A)])))) # [,1] [,2] #[1,] "w" NA #[2,] "x" "x" #[3,] "y" "y" #[4,] "z" NA

<?php

    if(isset($_POST['submit']))
    {
        if(getimagesize($_FILES['image']['tmp_name'])==FALSE)
        {
            echo "Please select an image";
        }
        else
        {
            $image = addslashes($_FILES['image']['tmp_name']);
            $image = file_get_contents($image);
            $image = base64_encode($image);
            saveimage($image);
        }
    }

    function saveimage($image)
    {
        $con=mysql_connect("localhost","root","");

        mysql_select_db("food",$con);

        $query = "INSERT INTO info(image) VALUES ('$image')";
        $result = mysql_query($query,$con);

        if($result)
        {
            echo "<br> Image upload";
        }
        else
        {
            echo "<br> NOT";
        }
    }

function displayimage()
    {
        $con=mysql_connect("localhost","root","");

        mysql_select_db("food",$con);

        $query = "SELECT image from info";
        $result = mysql_query($query,$con);
        $row=mysql_fetch_array($result);
    echo '<img src="data:image/jpeg;base64,'.$row["image"].'" width="200" height="200"/>';
    }



?>

答案 1 :(得分:0)

这很好用......

df1$B <- ifelse(df1$A %in% df2$B, as.character(df1$A), NA)

df3 <- merge(df1,df2,by='B',all.x = T,all.y = T)