我有两个数据框,下面是结构(列):
Price ["CID","Startdate","Enddate","P1","P2"]
Usage ["Date","Hour","CID","Value"]
我正在尝试将Price与Usage合并。我希望P1和P2值在' Usage'通过检查相应的“CID'和'日期' ' Price'
中的值这是我的R代码:
t_cid=Price$CID
Startdate=Price$Startdate
Enddate=Price$Enddate
fv <-function(x){
p<-Price[ (t_cid==x[3]) & (x[1] >=Startdate ) &(x[1] <=Enddate),c("P1","P2")]
return(list(p))
}
t1=Sys.time()
k=unlist(apply(Usage,1,fv))
t2=Sys.time()
print(t2-t1)
v=rep(c(TRUE,FALSE),length(k)/2)
Usage$P1=k[v]
Usage$P2=k[!v]
向量v分离来自k
的P1和P2值结果数据帧:用法
["Date","Hour","CID","Value","P1","P2"]
这个逻辑有效。但我想增加速度流量(6-10倍)。任何帮助都会非常感激。谢谢。
答案 0 :(得分:1)
<form method="POST" name="form1" action="<?php $_SERVER['PHP_SELF'];?>">
<table border="0">
<tr>
<td colspan="6"></td>
</tr>
<tr>
<td>
<select name="select_petugas1" style="width:18px;" onchange="this.form.submit('select_petugas1');"> //first combobox
<option></option>
<?php include 'dbconn.php';
$sql_peg1="SELECT * FROM users"; $result_peg1=$conn->query($sql_peg1);
while( $row_peg1=$result_peg1->fetch_assoc() ){
echo "<option>".$row_peg1['nama']."</option>";
}
?>
</select>
</td>
<td>
<?php
if(isset($_POST['select_petugas1'])){
$select_petugas1=$_POST['select_petugas1'];
echo "<input type='text' name='select_petugas1' value='".$select_petugas1."'>"; // Throw 1st result into the text box
$sql_NIP1="SELECT NIP FROM users WHERE nama='$select_petugas1'";
$result_NIP1=$conn->query($sql_NIP1);
$row_NIP1=$result_NIP1->fetch_assoc();
$NIP1=$row_NIP1['NIP'];
?>
</td>
<td> NIP</td>
<td>:</td>
<td><input type="text" name='NIP1' value="<?php echo $NIP1; ?>"></td>
</tr> <!-- child of first result -->
<tr>
<td colspan="5" bgcolor="blue"></td>
</tr>
<tr>
<td>
<select name="peg_2" style="width:18px;" onchange="submit(this)"><!--2nd combobox-->
<option></option>
<?php
$sql_peg2="SELECT nama FROM users";
$result_peg2=$conn->query( $sql_peg2 );
while ($row_peg2=$result_peg2->fetch_assoc()){
echo "<option value='".$row_peg2['nama']."'>".$row_peg2['nama']."</option>";
}
?>
</select>
</td>
<td>
<?php
if( isset($_POST['peg_2']) ){
$peg_2=$_POST['peg_2'];
echo "<input type='text' name='peg2' value='".$peg_2."'>"; // 2nd result throw into 2nd texbox
$sql_NIP2="SELECT NIP FROM users WHERE nama='$peg_2'";
$result_NIP2=$conn->query($sql_NIP2);
$row_NIP2=$result_NIP2->fetch_assoc();
?>
</td>
<td> NIP</td>
<td>:</td>
<td><input type='text' name='NIP2' value="<?php echo $row_NIP2['NIP'];?>"> <!--2nd child of result-->
<?php
}
}
if(isset($_POST['NIP2'])){
$NIP2=$_POST['NIP2'];
echo "<br /> NIP2 :".$NIP2."<br />";
}
mysqli_close($conn);
?>
</td>
</tr>
</table>
</form>
<form method="POST" name="wilayah" id="wilayah" action="<?php $_SERVER['PHP_SELF'];?>">
<table border="1">
<tr>
<td>
<select name="select_provinsi" onchange="submit(this)" style="width:18;">
<option selected>PROVINSI</option>
<?php
include 'dbconn.php';
$sql_prov="SELECT * FROM wilayah GROUP BY provinsi";
$result_prov=$conn->query($sql_prov);
echo "";
while($row_prov=$result_prov->fetch_assoc()){
$provinsi=$row_prov['provinsi'];
echo "<option value='".$provinsi."'>".$provinsi."</option>";
}
?>
</select>
<?php
if(isset($_POST['select_provinsi'])){
$select_provinsi=$_POST['select_provinsi'];
echo "
<input type='text' name='select_provinsi' value='".$select_provinsi."' placeholder='PROVINSI'>
</td>
</tr>";
$sql_kabkota="SELECT * FROM wilayah WHERE provinsi='$select_provinsi' GROUP BY kab_kota";
$result_kabkota=$conn->query($sql_kabkota);
?>
<tr>
<td>
<select name="select_kabkota" style="width:18px;" onchange="submit(this)"><option>KAB/KOTA</option>
<?php
while($row_kabkota=$result_kabkota->fetch_assoc()){
echo "<option>".$row_kabkota['kab_kota']."</option>";
}
?>
</select>
<?php
}
if(isset($_POST['select_kabkota'])){
$select_kabkota=$_POST['select_kabkota'];
?>
<input type="text" name="kab_kota" value="<?php echo $select_kabkota;?>">
<?php
}
mysqli_close($conn);
?>
</td>
</tr>
</table>
</form>
太棒了。
首先:下次请提供一些示例数据,以说明您的数据实际情况。它有助于查看可以使用的其他信息。我自己制作了一个样本,如果我或多或少得到它,请看看。
data.table
请注意,这些已定义为Price <- data.table(
CID = rep(c(1, 2), 1, each = 5),
Startdate = rep(seq(Sys.Date(), by = "2 day", length.out = 5), 2),
Enddate = rep(seq(Sys.Date() + 1, by = "2 day", length.out = 5), 2),
P1 = runif(10), P2 = rnorm(10)
)
Usage = data.table(
Date = rep(seq(Sys.Date(), by = "2 day", length.out = 4), 2),
Hour = 6:13,
CID = rep(c(1, 2),2, each = 2),
Value = runif(8)
)
。只需运行data.tables
即可从任何dt <- data.table(df)
data.table
所以这是你的交易:
data.frame
作出的承诺:Usage[ , P1 := Price[.BY$Date >= Startdate & .BY$Date <= Enddate & CID == .BY$CID, P1], by = c("Date", "CID")]
Usage[ , P2 := Price[.BY$Date >= Startdate & .BY$Date <= Enddate & CID == .BY$CID, P2], by = c("Date", "CID")]
中的Price
和CID
的每个组合Date
只有一行。
查看Usage
文档以获取更多信息,因为这可能会破坏这篇文章。
玩得开心!