重塑我的数据框并添加标志

时间:2016-08-09 01:49:42

标签: r dplyr reshape

我有一个如下所示的数据框:

df <- 
ID   TIME   IPREDP   IPREDM
1     0.5    10        5
1     1.0    15        7
2     0.7    8         2

我想重塑我的数据并将IPREDPIPREDM列折叠到一个名为IPRED的列中,并为每个列添加一个名为DVID的标记,并删除原始IPREDPIPREDM列。所以输出应该是这样的:

dfout <- 
ID    TIME    DVID    IPRED
1     0.5     1       10
1     0.5     2       5
1     1.0     1       15
1     1.0     2       7
2     0.7     1       8
2     0.7     2       2

如何以最快的方式在R中实现这一目标?

3 个答案:

答案 0 :(得分:2)

<?php
session_start();
include_once "Dbconnect.php";
if(isset($_POST['btn_up'])){
  $email = $_POST['sn'];
  if($email == ''){
    echo 'there was an error';
  }else{
    echo $email;
    echo "something happened";
  }
}
var_dump($email);
?>
<!DOCTYPE html>
<html>
<head>
<title> Admin </title>
</head>
<body>

  <form method='post'>
    <label for="select">School name</label>
    <select name="sn">
        <option value="-" selected data-default>-</option>
        <?php
            $query = mysql_query("SELECT * FROM users WHERE school = 'sn'");
            while($rowtwo = mysql_fetch_array($query)){
                ?>
                <option value="<?php echo $rowtwo['email']; ?>"><?php echo $rowtwo['email']; ?></option>
                <?php
            }
        ?>
    </select>

<button type="submit" name='btn_up'>up</button>
</form>

</body>
</html>

答案 1 :(得分:2)

   library("data.table") 
   df1 <- melt(df, id = c("ID", "TIME"), measure  = c("IPREDP", "IPREDM"), value.name = "IPRED")
   df1$DVID <- as.integer(df1$variable)
   df1[, variable := NULL]

答案 2 :(得分:1)

我们可以使用RichTextBlock

中的melt
data.table