我希望在每一行之前和之后的两列中出现的值运行累积和。因此,在这种情况下,我在两天内每分钟都有2种事件类型。我想创建一个列,它按类型添加每行之前和之后发生的所有事件。我想到了来自excel的Sumif,但我不确定如何将它移植到R:
编辑:添加了set.seed和更简单的数字
我有以下数据集:
master_min incident1 incident2
1: 2016-01-01 00:00:00 9 6
2: 2016-01-01 00:01:00 9 5
3: 2016-01-01 00:02:00 3 5
4: 2016-01-01 00:03:00 8 6
5: 2016-01-01 00:04:00 6 9
我如何实质上计算以下逻辑:
对于每一行,将该行时间戳之前发生的所有事件1和该行时间戳之后发生的所有事件2相加?这样会很好数据表解决方案,如果不是dplyr,因为我正在使用大型数据集。以下是数据`之前和之后:
在:
master_min incident1 incident2 new_column
1: 2016-01-01 00:00:00 9 6 25
2: 2016-01-01 00:01:00 9 5 29
3: 2016-01-01 00:02:00 3 5 33
4: 2016-01-01 00:03:00 8 6 30
5: 2016-01-01 00:04:00 6 9 29
计算后:
Js/jquery.js
Js/jquery-2.js
Js/jquery-3.js
答案 0 :(得分:0)
*更新
以下两行可以完成这项工作
master_min$sum1 <- cumsum(master_min$incident1)
master_min$sum2 <- sum(master_min$incident2) - cumsum(master_min$incident2)
我重写了一下这个问题,以显示更全面的结构
library(data.table)
master_min <-
setDT(
data.frame(master_min = seq(
from=as.POSIXct("2016-1-1 0:00", tz="America/New_York"),
to=as.POSIXct("2016-1-1 0:09", tz="America/New_York"),
by="min"
))
)
set.seed(2)
incident1= as.integer(runif(10, min=0, max=10))
incident2= as.integer(runif(10, min=0, max=10))
master_min = cbind(master_min, incident1, incident2)
现在master_min看起来像这样
> master_min
master_min incident1 incident2
1: 2016-01-01 00:00:00 1 5
2: 2016-01-01 00:01:00 7 2
3: 2016-01-01 00:02:00 5 7
4: 2016-01-01 00:03:00 1 1
5: 2016-01-01 00:04:00 9 4
6: 2016-01-01 00:05:00 9 8
7: 2016-01-01 00:06:00 1 9
8: 2016-01-01 00:07:00 8 2
9: 2016-01-01 00:08:00 4 4
10: 2016-01-01 00:09:00 5 0
应用转换
master_min$sum1 <- cumsum(master_min$incident1)
master_min$sum2 <- sum(master_min$incident2) - cumsum(master_min$incident2)
结果
> master_min
master_min incident1 incident2 sum1 sum2
1: 2016-01-01 00:00:00 1 5 1 37
2: 2016-01-01 00:01:00 7 2 8 35
3: 2016-01-01 00:02:00 5 7 13 28
4: 2016-01-01 00:03:00 1 1 14 27
5: 2016-01-01 00:04:00 9 4 23 23
6: 2016-01-01 00:05:00 9 8 32 15
7: 2016-01-01 00:06:00 1 9 33 6
8: 2016-01-01 00:07:00 8 2 41 4
9: 2016-01-01 00:08:00 4 4 45 0
10: 2016-01-01 00:09:00 5 0 50 0
答案 1 :(得分:0)
如果我理解正确:
if (isset($_GET['id'])) {
// Connect to the MySQL database
include "storescripts/connect_to_mysql.php";
$id = preg_replace('#[^0-9]#i', '', $_GET['id']);
//1. Store only differents ids in the session by creating an associative array
$_SESSION['lastViewProducts'][$id] = $id;
//3. Don't show the $_GET['id'] in the list
$ids = array_filter($_SESSION['lastViewProducts'], function($currentID) use($id) {
return $id != $currentId;
});
//Keep only 5 distincts articles
$ids = array_slice(array_unique($ids), 0, 5);
//2. declare your string OUTSIDE the foreach
$lastVIEWproduct = '';
foreach($ids as $valueLASTview) {
$stmtLastVIEW = $con->prepare('SELECT id, product_name, price, details, category, subcategory, size, date_added, image, brand_name, product_color, vizualizari FROM products WHERE id=?');
$stmtLastVIEW->bind_param('i', $valueLASTview);
$stmtLastVIEW->execute();
$stmtLastVIEW->bind_result($idSelectDetalii, $produsNumeDetalii, $priceDetalii, $produsDetalii, $produsCategory, $produsSubcategory, $produsSize, $produsDate_added, $imageLocationDetalii, $brandProdusSelectat, $produsColor, $produsVizualizari);
while ($stmtLastVIEW->fetch()) {
$lastVIEWproduct .='titlu: '.$idSelectDetalii.' <img src="'.$imageLocationDetalii.'" class="img-responsive">';
}
$stmtLastVIEW->free_result();
}
}