计算在特定时间运行的报告数量

时间:2018-11-08 19:45:04

标签: r algorithm data-manipulation

我正在尝试计算某个时间正在运行的报告数量。

数据类似于:

ReportID    StartTime   Duration  
   1 2018-11-02 13:00:00  240 seconds  
   2 2018-11-02 14:00:00  300 seconds  
   3 2018-11-02 14:01:15  300 seconds  
   4 2018-11-02 14:00:00 5000 seconds

理想的输出将是:

Time #ReportsRunning
2018-11-01 13:00:00 0
2018-11-02 13:00:00 1  
2018-11-02 14:00:00 2
2018-11-02 15:00:00 1

反正有做这样的事情吗?我正在考虑将列添加到要检查的每个时间戳。但这会使表格变得非常宽。


可复制形式的数据:

df1 <- data.frame(
  ReportID = 1:4,
  StartTime = as.POSIXct(c("2018-11-02 13:00:00", "2018-11-02 14:00:00",
                           "2018-11-02 14:01:15", "2018-11-02 14:00:00")),
  Duration = as.difftime(c(240, 300, 300, 5000), units = "secs")
)

df2 <- data.frame(
  Time = as.POSIXct(c("2018-11-01 13:00:00", "2018-11-02 13:00:00",
                      "2018-11-02 14:00:00", "2018-11-02 15:00:00"))
)

1 个答案:

答案 0 :(得分:0)

这是基本的R解决方案:

package sample.Controllers;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import sample.Classes.DialogManager;
import sample.ControllerMain;

public class ControllerFind {

    //Window "Find"
    @FXML public TextField searchTextField;
    @FXML public Label findTextLabel;
    @FXML public Button okTextFindButton;
    @FXML public Button cancelTextFindButton;
    private String text;
    private ControllerMain controller;

    public void setParent (ControllerMain controller){
        this.controller = controller;
    }

    public ControllerFind getThis(){
        return this;
    }

    public void initialize(){
        System.out.println("psvm");
    }

    public void textFindOkButtonAction(ActionEvent actionEvent) {
        text = (searchTextField.getText());
        if (text.equals("")) {
            DialogManager.showInfoDialog("Error!", "Enter text what you are looking for!");
        } else {
            if (controller.textAreaOne.getText() != null && !controller.textAreaOne.getText().isEmpty()) {
                int index = controller.textAreaOne.getText().indexOf(text);
                if (index == -1) {
                    DialogManager.showInfoDialog("Result", "There isn't text what you are looking for");
                } else {
                    controller.textAreaOne.selectRange(index, index + text.length());
                }
            } else {
                DialogManager.showInfoDialog("Error", "TextArea is empty!");
            }
        }
    }

    public void textFindCancelButtonAction(ActionEvent actionEvent) {
        Node source = (Node) actionEvent.getSource();
        Stage stage = (Stage) source.getScene().getWindow();
        stage.close();
    }
}

但是,如果您的数据很大,则使用BioConductor的IRanges package应该会更有效率:

df2$`#ReportsRunning` <- sapply(
  df2$Time,
  function(x) sum(x >= df1$StartTime & x <= df1$StartTime + df1$Duration)
)

df2
#                  Time #ReportsRunning
# 1 2018-11-01 13:00:00               0
# 2 2018-11-02 13:00:00               1
# 3 2018-11-02 14:00:00               2
# 4 2018-11-02 15:00:00               1