按小时汇总时间并计算它

时间:2017-07-28 06:24:22

标签: r dataframe

我在R。

中有以下数据框
 Date                    Car_NO
 2016-12-24 19:35:00       ABC
 2016-12-24 19:55:00       DEF
 2016-12-24 20:15:00       RTY
 2016-12-24 20:35:00       WER
 2016-12-24 21:34:00       DER
 2016-12-24 00:23:00       ABC
 2016-12-24 00:22:00       ERT
 2016-12-24 11:45:00       RTY
 2016-12-24 13:09:00       RTY

Date format is "POSIXct" "POSIXt"

我想计算每小时的车流量。像12-1,1-2,2-3,3-4等等

目前我的方法是

df$time <- ymd_hms(df$Date)

df$hours <- hour(df$time) 
df$minutes <- minute(df$time)

df$time <- as.numeric(paste(df$hours,df$minutes,sep="."))

在此之后,我将应用ifelse循环在每小时的时间段划分它,但我认为这将是漫长而乏味的方式。 R中有没有简单的方法。

我想要的数据框是

 Time_Slots      Car_Traffic_count
   00-01               2
   01-02               0
   02-03               0
    .              
    .
    .
   19-20               2
   20-21               2
   21-22               1
    .
    .
    .

1 个答案:

答案 0 :(得分:2)

最简单的方法是使用开始时间来表示时间间隔:

create or replace PROCEDURE UPLOAD_CMS_VISA_RR
AS
BEGIN
  DECLARE
   f utl_file.file_type;
s                  VARCHAR2(4000);
BRANCH_CODE        VARCHAR2(500);
TRANS_DATE         VARCHAR2(500);
TRANS_TIME         VARCHAR2(500);
SEQ_NO             VARCHAR2(500);
MSG_TYPE           VARCHAR2(500);
REMOTE_ACCOUNT     VARCHAR2(500);
FRG_CURR_AMT       VARCHAR2(500);
LOC_CURR_AMT       VARCHAR2(500);
CHEQUE_NO          VARCHAR2(500);
IBCA_NO            VARCHAR2(500);
CR_DR_CODE         VARCHAR2(500);
DESC_DETAIL        VARCHAR2(500);
DOC_NO             VARCHAR2(500);
Y_LIVECMGT_ID      VARCHAR2(500);
FILLER_ONE         VARCHAR2(500);
T24_ACC_NO         VARCHAR2(500);
FILLER_TWO         VARCHAR2(500);
INST_CODE          VARCHAR2(500);
FILLER             VARCHAR2(500);
LAST_DATE_MODIFIED VARCHAR2(500);
LEAD_COMPANY       VARCHAR2(500);
LEAD_COUNTRY       VARCHAR2(500);
V_TABLE_RECORD_COUNT NUMBER :=0;
V_FILE_RECORD_COUNT NUMBER :=0;
-- start_pos NUMBER;
ex    BOOLEAN;
flen  NUMBER;
bsize NUMBER;
CURSOR C1
IS
  SELECT FILENAME
  FROM DIR_LIST2
  WHERE FILENAME = 'DESTRNFL.20170726.0401671434.00';
BEGIN
GET_DIR_LIST2( '/cdb/CSHMGMT' );
FOR FILE_REC IN C1
LOOP
  UTL_FILE.FGETATTR('CSHMGMT', FILE_REC.FILENAME, EX, FLEN, BSIZE);
  DBMS_OUTPUT.PUT_LINE(FILE_REC.FILENAME);
  IF EX THEN

  V_TABLE_RECORD_COUNT :=0;
  V_FILE_RECORD_COUNT:= 0;

    dbms_output.put_line('File Exist');
    F := UTL_FILE.FOPEN('CSHMGMT', FILE_REC.FILENAME, 'R');
    LOOP
      BEGIN
      V_FILE_RECORD_COUNT := V_FILE_RECORD_COUNT + 1;

        UTL_FILE.GET_LINE(F,S);
        BRANCH_CODE        := (SUBSTR(s, 1, 4));
        TRANS_DATE         := (SUBSTR(S, 5,8));
        TRANS_TIME         := (SUBSTR(S, 13,6));
        SEQ_NO             := (SUBSTR(S,19,10));
        MSG_TYPE           := (SUBSTR(S,29,4));
        REMOTE_ACCOUNT     := (SUBSTR(S,33,10));
        FRG_CURR_AMT       := (SUBSTR(S,43,14));
        LOC_CURR_AMT       := (SUBSTR(S,57,14));
        CHEQUE_NO          := (SUBSTR(S,71,8));
        IBCA_NO            := (SUBSTR(S,79,8));
        CR_DR_CODE         := (SUBSTR(S,87,1));
        DESC_DETAIL        := (SUBSTR(s,88,21));
        DOC_NO             := (SUBSTR(S,109,6));
        Y_LIVECMGT_ID      := (SUBSTR(s,115,16));
        FILLER_ONE         := (SUBSTR(S,131,65));
        T24_ACC_NO         := (SUBSTR(s,196,16));
        FILLER_TWO         := (SUBSTR(s,212,199));
        INST_CODE          := (SUBSTR(S,411,6));
        FILLER             := (SUBSTR(s,417));
        LAST_DATE_MODIFIED := NULL;
        LEAD_COMPANY       := 001;
        LEAD_COUNTRY       := 'PK';
        INSERT
        INTO CMS_VISA_TEST_RR
          (
            BRANCH_CODE,
            TRANS_DATE,
            TRANS_TIME,
            SEQ_NO,
            MSG_TYPE,
            REMOTE_ACCOUNT,
            FRG_CURR_AMT,
            LOC_CURR_AMT,
            CHEQUE_NO,
            IBCA_NO,
            CR_DR_CODE,
            DESC_DETAIL,
            DOC_NO,
            Y_LIVECMGT_ID,
            FILLER_ONE,
            T24_ACC_NO,
            FILLER_TWO,
            INST_CODE,
            FILLER,
            LAST_DATE_MODIFIED,
            LEAD_COMPANY,
            LEAD_COUNTRY,
            SYSTEM_DATE
          )
          VALUES
          (
            TRIM(BRANCH_CODE),
            CONVERT_TO_DATE(TRANS_DATE),
            TRIM(TRANS_TIME),
            TRIM(SEQ_NO),
            TRIM(MSG_TYPE),
            TRIM(REMOTE_ACCOUNT),
            TRIM(FRG_CURR_AMT),
            TRIM(LOC_CURR_AMT),
            TRIM(CHEQUE_NO),
            TRIM(IBCA_NO),
            TRIM(CR_DR_CODE),
            TRIM(DESC_DETAIL),
            TRIM(DOC_NO),
            TRIM(Y_LIVECMGT_ID),
            TRIM(FILLER_ONE),
            TRIM(T24_ACC_NO),
            TRIM(FILLER_TWO),
            TRIM(INST_CODE),
            TRIM(FILLER),
            CONVERT_TO_DATE(LAST_DATE_MODIFIED),
            LEAD_COMPANY,
            LEAD_COUNTRY,
            SYSDATE
          );
        COMMIT;

        V_TABLE_RECORD_COUNT := V_TABLE_RECORD_COUNT +1;
        --END IF;
             EXCEPTION
      WHEN utl_file.INVALID_PATH THEN
        DBMS_OUTPUT.PUT_LINE('Invalid Path');
        ERROR_LOGGER ('UPLOAD_CMS_VISA_RR', 'Invalid Pat','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN utl_file.INVALID_MODE THEN
        dbms_output.put_line('Invalid Mode');
        ERROR_LOGGER ('UPLOAD_CMS_VISA_RR', 'Invalid Mod','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN UTL_FILE.INVALID_FILEHANDLE THEN
        dbms_output.put_line('Invalid File Handler');
        ERROR_LOGGER ('UPLOAD_CMS_VISA_RR', 'Invalid File Handler','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN utl_file.INVALID_OPERATION THEN
        DBMS_OUTPUT.PUT_LINE('Invalid Operation');
        ERROR_LOGGER ('UPLOAD_CMS_VISA_RR', 'Invalid Operation','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN utl_file.READ_ERROR THEN
        DBMS_OUTPUT.PUT_LINE('Read Error');
        ERROR_LOGGER ('UPLOAD_CMS_VISA_RR', 'Read Error','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN utl_file.WRITE_ERROR THEN
        DBMS_OUTPUT.PUT_LINE('Write Error');
        ERROR_LOGGER ('UPLOAD_CMS_VISA_RR', 'Write Error','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN utl_file.INTERNAL_ERROR THEN
        DBMS_OUTPUT.PUT_LINE('Internal Error');
        ERROR_LOGGER ('UPLOAD_CMS_VISA_RR', 'Internal Error','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN VALUE_ERROR THEN
        DBMS_OUTPUT.PUT_LINE('Value error');
        ERROR_LOGGER ('UPLOAD_CMS_VISA_RR', 'Value error','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
      WHEN NO_DATA_FOUND THEN
        ERROR_LOGGER ('UPLOAD_CMS_VISA_RR', 'NO_DATA_FOUND','FILE_NAME='|| FILE_REC.FILENAME||' TRANS_TIME='|| TRIM(TRANS_TIME)||' LOC_CURR_AMT='||TRIM(LOC_CURR_AMT)||' TT NO='||Y_LIVECMGT_ID, SQLCODE, SUBSTR(SQLERRM, 1, 500));
        --    commit;
        --              utl_file.fcopy('CDB_UPLOAD', 'export.log', 'CDB_UPLOAD', 'export.log.processed');
        --              utl_file.frename('CSHMGMT', FILE_REC.FILENAME, 'CMS_DHA', FILE_REC.FILENAME|| '_' || SYSDATE);
        --              utl_file.frename('PHXEXPORT', 'export.log', 'PHXEXPORT', 'export.log.processed' || SYSDATE);
        EXIT;
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE('Other Error');
        CONTINUE;
      END;
    END LOOP;
    --INSERT  INTO CMS_CONTROL_RR ( FILENAME,TABLE_RECORD_COUNT,FILE_RECORD_COUNT, SYSTEM_DATE ) VALUES ( FILE_REC.FILENAME,V_TABLE_RECORD_COUNT,V_FILE_RECORD_COUNT,  SYSDATE  );
    --COMMIT;
    --UTL_FILE.FREMOVE ('CSHMGMT', FILE_REC.FILENAME);
    utl_file.fclose(f);
  END IF;
END LOOP;
COMMIT;
  EXCEPTION
  WHEN NO_DATA_FOUND THEN
ERROR_LOGGER ('UPLOAD_CMS_VISA_RR', 'CMS_VISA_TEST_RR', '', SQLCODE, SUBSTR(SQLERRM, 1, 500));
  WHEN OTHERS THEN
ROLLBACK;
ERROR_LOGGER ('UPLOAD_CMS_VISA_RR', 'CMS_VISA_TEST_RR', '', SQLCODE,    SUBSTR(SQLERRM, 1, 500));
END;
    END UPLOAD_CMS_VISA_RR;

输出:

# sample data
df = data.frame(time = Sys.time()+seq(1,10)*10000, runif(10) )

# summarize
library(dplyr)
df$hour = factor(as.numeric(format(df$time,"%H")), levels = seq(0,24))
df = df %>% 
      group_by(hour) %>% 
      summarize(count=n()) %>% 
      complete(hour, fill = list(count = 0))

您可以选择添加:

# A tibble: 24 x 2
     hour count
   <fctr> <dbl>
 1      0     0
 2      1     1
 3      2     0
 4      3     0
 5      4     1
 6      5     0
 7      6     1
 8      7     0
 9      8     0
10      9     1
# ... with 14 more rows

然后结束以获得所需的格式。希望这有帮助!