我有以下数据框。
正如许多用户所建议的那样,我与dput分享这个。
> dput(time_interval_df)
structure(list(timeinterval = structure(1:24, .Label = c("00:00:00 00:59:59",
"01:00:00 01:59:59", "02:00:00 02:59:59", "03:00:00 03:59:59",
"04:00:00 04:59:59", "05:00:00 05:59:59", "06:00:00 06:59:59",
"07:00:00 07:59:59", "08:00:00 08:59:59", "09:00:00 09:59:59",
"10:00:00 10:59:59", "11:00:00 11:59:59", "12:00:00 12:59:59",
"13:00:00 13:59:59", "14:00:00 14:59:59", "15:00:00 15:59:59",
"16:00:00 16:59:59", "17:00:00 17:59:59", "18:00:00 18:59:59",
"19:00:00 19:59:59", "20:00:00 20:59:59", "21:00:00 21:59:59",
"22:00:00 22:59:59", "23:00:00 23:59:59"), class = "factor"),
timecount = c(1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1,
0, 0, 0, 0, 1, 0, 1, 1, 1)), .Names = c("timeinterval", "timecount"
), row.names = c(NA, -24L), class = "data.frame")
我的数据框的结构是
str(time_interval_df)
'data.frame': 24 obs. of 2 variables:
$ timeinterval: Factor w/ 24 levels "00:00:00 00:59:59",..: 1 2 3 4 5 6 7 8 9 10 ...
$ timecount : num 1 1 1 0 0 0 0 0 1 1 ...
如果timeinterval之间的差值为1并且有一些时间计数,那么我想将它们分组到一个组中。例如,差异b / w行没有1和行没有2是1而且差异是b / w row2和row3(连续)是1所以我想将all分组为一个并显示新行前面的timecount之和,即" 00:00:00 02:59:59" 以下是我想要的输出
获得第二个输出后,我的第二个期望结果将是
请帮我解决这个问题。我花了很多时间在这里。
答案 0 :(得分:2)
我们可以使用if (req.query.productType) {
var stringObj = _.clone(jsonFile);
stringObj.productList = filterByProduct(jsonFile, req.query.productType);
stringObj.FilteredProducts = stringObj.productList.length;
res.end(stringObj);
}
。将'data.frame'转换为'data.table'(data.table
)。通过在空白处拆分'timeinterval(setDT(df1
)来创建两列('time1','time2'),按照'timecount'的运行长度ID进行分组,tstrsplit()
{{1} 'timecount'值不等于0,然后if
分别是'time1'和'time2'的第一个(all
)和最后一个(paste
)元素,并且还得到一个或多个行(1
)。如果需要,将'grp'列指定为'NULL'
.N
从'res1',我们可以总结得到'res2'
.N