2017-06-13 18 views
4

大規模なデータセット内の業務日に15分間隔で開始されたセッションの数をカウントしたいと思います。R:15分間隔でカウントする

df <- 

Start_datetime  End_datetime  Duration Volume 
2016-04-01 06:20:55 2016-04-01 14:41:22 08:20:27 8.360 
2016-04-01 08:22:27 2016-04-01 08:22:40 00:00:13 0.000 
2016-04-01 08:38:53 2016-04-01 09:31:58 00:53:05 12.570 
2016-04-01 09:33:57 2016-04-01 12:37:43 03:03:46 7.320 
2016-04-01 10:05:03 2016-04-01 16:41:16 06:36:13 9.520 
2016-04-01 12:07:57 2016-04-02 22:22:32 34:14:35 7.230 
2016-04-01 16:56:55 2016-04-02 10:40:17 17:43:22 5.300 
2016-04-01 17:29:18 2016-04-01 19:50:29 02:21:11 7.020 
2016-04-01 17:42:39 2016-04-01 19:45:38 02:02:59 2.430 
2016-04-01 17:47:57 2016-04-01 20:26:35 02:38:38 8.090 
2016-04-01 22:00:15 2016-04-04 08:22:21 58:22:06 4.710 
2016-04-02 01:12:38 2016-04-02 09:49:00 08:36:22 3.150 
2016-04-02 01:32:00 2016-04-02 12:49:47 11:17:47 5.760 
2016-04-02 07:28:48 2016-04-04 06:58:56 47:30:08 0.000 
2016-04-02 07:55:18 2016-04-05 07:55:15 71:59:57 0.240 

私は、開始15分ごとに全ての出発セッションをカウントしたい場所::

私のデータは次のようになります週末の

For business days 
    Time    PTU Count 
    00:00:00 - 00:15:00 1  10  #(where count is the amount of sessions started between 00:00:00 and 00:15:00) 
    00:15:00 - 00:30:00 2  6 
    00:30:00 - 00:45:00 3  5 
    00:45:00 - 01:00:00 3  3 

というように、同じデータ。

私はカット機能を試してみました:

df$PTU <- table (cut(df$Start_datetime, breaks="15 minutes")) 
data.frame(PTU) 

は編集:私はこれを実行すると、私は、次のエラーが表示さ:

Error in cut.default(df$Start_datetime, breaks = "15 minutes") :'x' must be numeric 

そしてlubridateといくつかの機能を、私はように見えることはできませんそれを機能させる。私の最終的な目標は、次のようなテーブルを作成することですが、15分間隔でテーブルを作成します。
enter image description here

+1

実際の処理である 'cut'アプローチは – akrun

+1

に動作していない理由を説明してもらえますが、' 1ビットのデータをdput'てもらえますか? –

+1

営業日をお探しの場合は、[こちら](https://cran.r-project.org/web/packages/bizdays/bizdays.pdf)をご確認ください – akrun

答えて

1

あなたは日付時刻にcutを使用するときに留意する必要が二つあります:

  1. は、あなたのデータが実際にPOSIXtクラスであることを確認してください。私はあなたが本当にそうでないと確信しています、またはRはcut.defaultではなくcut.POSIXtをメソッドとして使用していません。
  2. "15 minutes"は、"15 min"である必要があります。出力はあなたのテーブルの名前など、15分間隔の開始を与えること

    Start_datetime <- as.POSIXct(
        c("2016-04-01 06:20:55", 
        "2016-04-01 06:22:12", 
        "2016-04-01 05:30:12") 
    ) 
    
    table(cut(Start_datetime, breaks = "15 min")) 
    # 2016-04-01 05:30:00 2016-04-01 05:45:00 2016-04-01 06:00:00 2016-04-01 06:15:00 
    #     1     0     0     2 
    

    注:だからこの作品?cut.POSIXt

を参照してください。

1

ここでは、datetime "strings"から必要な形式への完全な処理の一種です。スタートは、文字列のベクトルである:

Start_time <- 
c("2016-04-01 06:20:55", "2016-04-01 08:22:27", "2016-04-01 08:38:53", 
    "2016-04-01 09:33:57", "2016-04-01 10:05:03", "2016-04-01 12:07:57", 
    "2016-04-01 16:56:55", "2016-04-01 17:29:18", "2016-04-01 17:42:39", 
    "2016-04-01 17:47:57", "2016-04-01 22:00:15", "2016-04-02 01:12:38", 
    "2016-04-02 01:32:00", "2016-04-02 07:28:48", "2016-04-02 07:55:18" 
) 
df <- data.frame(Start_time) 

そして、これは

## We will use two packages 
library(lubridate) 
library(data.table) 

# convert df to data.table, parse the datetime string 
setDT(df)[, Start_time := ymd_hms(Start_time)] 
# floor time by 15 min to assign the appropriate slot (new variable Start_time_slot) 
df[, Start_time_slot := floor_date(Start_time, "15 min")] 

# aggregate by wday and time in a date 
start_time_data_frame <- df[, .N, by = .(wday(Start_time_slot), format(Start_time_slot, format="%H:%M:%S"))] 

# output looks like this 
start_time_data_frame 
##  wday  time N 
## 1: 6 06:15:00 1 
## 2: 6 08:15:00 1 
## 3: 6 08:30:00 1 
## 4: 6 09:30:00 1 
## 5: 6 10:00:00 1 
## 6: 6 12:00:00 1 
## 7: 6 16:45:00 1 
## 8: 6 17:15:00 1 
## 9: 6 17:30:00 1 
## 10: 6 17:45:00 1 
## 11: 6 22:00:00 1 
## 12: 7 01:00:00 1 
## 13: 7 01:30:00 1 
## 14: 7 07:15:00 1 
## 15: 7 07:45:00 1 
関連する問題