2017-03-18 7 views
0

私は現在、食品トラックのデータを扱っており、申請者、開かれている曜日、開始時間と終了時間によって整理されています。時間帯の行を作成する

開始時間と終了時間の範囲内で1時間が発生した場合は、別の行または列を作成するかどうかを尋ねられました(開く、開いていない)。

Rに返すようにする方法はありますか?毎時間、開始時間と終了時間の範囲内にある時間とラベルを開きます。そして、範囲内にない毎時でそれを同じことに尋ねて、「Not Open」というラベルを付けます。

forループを使用しようとしましたが、成功しませんでした。

for(Yes in c("1","2","3","4","5","6","7","8","9","10","11","12","13","14", 
      "15","16","17","18","19","20","21","22","23","24")) 
{ 
    print(Yes) 
    if(Yes %in% (NSFS$starthour %between% NSFS$endhour)) 
} 



DayOfWeekStr Applicant   starthour endhour locationid 
Friday  Natan's Catering 12   13  437207 
Friday  Linda's Catering 10   15  760539 
Wednesday Mang Hang Catering 12   13  559779 
Sunday  Tacos Santana  17   22  453014 
Friday  Breaking Bread Inc. 14   18  934995 
+0

終了時間は何ですか? –

+0

の質問で混乱します。例えば、開始時間は14、終了時間は22になるので、食品トラックは午後2時から午後10時までオープンします。 –

答えて

0

私はあなたが開始と終了の間のすべての時間を見つけるためにseqを使用することができます

# applicant day start_hour end_hour 
#1   a monday   9  10 
#2   a monday   12  12 
#3   a monday   14  16 
#4   a monday   17  18 

:例えば、あなたがSTART_HOURとEND_HOURは整数で入力テーブルを持っていると仮定します。次のコードでは、営業時間(dt_open_hours)とdata.tabledt_all_hours)の1つを可能な限りすべての時間(入力データの申請者と日数を使用)で生成することを考えています。 2つのデータテーブルをマージすることにより、得られるテーブルにはapplicant,dayおよびhourのすべての可能な組み合わせが含まれますが、状態(Open/Not Open)はdt_open_hoursからのみ発生します。最後のステップは、 "開かない" に欠損値(NA)を変換することです:

library(data.table) 
dt <- structure(list(applicant = structure(c(1L, 1L, 1L, 1L), .Label = "a", class = "factor"), 
    day = structure(c(1L, 1L, 1L, 1L), .Label = "monday", class = "factor"), 
    start_hour = c(9L, 12L, 14L, 17L), end_hour = c(10L, 12L, 
    16L, 18L)), .Names = c("applicant", "day", "start_hour", 
    "end_hour"), class = "data.frame", row.names = c(NA, -4L)) 

# Convert to data.table 
setDT(dt) 
# Assign row_id unique row_ids for seq to work on one row at a time 
dt[, row_id := seq(1, nrow(dt))] 
# Convert start and end hour into sequence of hours between start and end 
dt_open_hours <- dt[, .(state = "Open", 
         hour = as.integer(seq(from = start_hour, to = end_hour, by = 1))), 
    by = .(row_id, applicant, day)] 
# Remove row_id column 
dt_open_hours[, row_id := NULL] 
# Generate data.table with all combinations of applicant, day and hour 
dt_all_hours <- CJ(applicant = unique(dt_open_hours[, applicant]), 
        day = unique(dt_open_hours[, day]), hour = seq(1, 24)) 
# Merge 
out <- dt_open_hours[dt_all_hours, on=.(applicant, day, hour)] 
out[is.na(state), state := "Not Open"] 

out data.tableは次のようになります。次の更新の入力データを使用して:

# applicant day state hour 
# 1:   a monday Not Open 1 
# 2:   a monday Not Open 2 
# 3:   a monday Not Open 3 
# 4:   a monday Not Open 4 
# 5:   a monday Not Open 5 
# 6:   a monday Not Open 6 
# 7:   a monday Not Open 7 
# 8:   a monday Not Open 8 
# 9:   a monday  Open 9 
#10:   a monday  Open 10 
#11:   a monday Not Open 11 
#12:   a monday  Open 12 
#13:   a monday Not Open 13 
#14:   a monday  Open 14 
#15:   a monday  Open 15 
#16:   a monday  Open 16 
#17:   a monday  Open 17 
#18:   a monday  Open 18 
#19:   a monday Not Open 19 
#20:   a monday Not Open 20 
#21:   a monday Not Open 21 
#22:   a monday Not Open 22 
#23:   a monday Not Open 23 
#24:   a monday Not Open 24 
# applicant day state hour 

更新.frame:

DayOfWeekStr   Applicant starthour endhour locationid 
1  Friday Natan's Catering  12  13  437207 
2  Friday Linda's Catering  10  15  760539 
3 Wednesday Mang Hang Catering  12  13  559779 
4  Sunday  Tacos Santana  17  22  453014 
5  Friday Breaking Bread Inc.  14  18  934995 

コードにいくつかの変更が加えられました(i N更新入力data.frame)は、主にlocationid変数を組み込むこと:dt_open_hoursは、DayOfWeekStrdt_all_hoursが同じために24時間に1が含ま、locationidlocationid)が新しく、Applicantの一意の組合せごとにstarthourendhour間の時間が含まれていますApplicant,DayOfWeekStr,locationidのユニークな組み合わせ。 dt_open_hoursdt_all_hoursのマージは、Applicant,DayOfWeekStr,locationidおよびhourlocationidは新規)で行われます。

library(data.table) 
dt <- structure(list(DayOfWeekStr = c("Friday", "Friday", "Wednesday", 
    "Sunday", "Friday"), Applicant = c("Natan's Catering", "Linda's Catering", 
    "Mang Hang Catering", "Tacos Santana", "Breaking Bread Inc."), 
    starthour = c(12L, 10L, 12L, 17L, 14L), endhour = c(13L, 
    15L, 13L, 22L, 18L), locationid = c(437207L, 760539L, 559779L, 
    453014L, 934995L)), .Names = c("DayOfWeekStr", "Applicant", 
    "starthour", "endhour", "locationid"), row.names = c(NA, -5L), 
    class = "data.frame") 

# Convert to data.table 
setDT(dt) 
# Assign row_id unique row_ids for seq to work on one row at a time 
dt[, row_id := seq(1, nrow(dt))] 
# Convert start and end hour into sequence of hours between start and end 
dt_open_hours <- dt[, .(state = "Open", 
         hour = as.integer(seq(from = starthour, to = endhour, by = 1))), 
        by = .(row_id, Applicant, DayOfWeekStr, locationid)] 
# Remove row_id column 
dt_open_hours[, row_id := NULL] 
# Generate data.table with all combinations of applicant, day and hour 
dt_all_hours <- dt[, .(hour = seq(1, 24)), 
        by = . (Applicant, DayOfWeekStr, locationid)] 
# Merge 
out <- dt_open_hours[dt_all_hours, 
        on=.(Applicant, DayOfWeekStr, locationid, hour)] 
out[is.na(state), state := "Not Open"] 
+0

ありがとうございます、この例は完璧に機能します!私はしかし、1つの問題があります。私は約500の変数を持っているので、それらをすべて手で入力するのは好都合ではありません。 Rに各変数を渡して情報を選択させる方法はありますか? –

+0

あなたは大歓迎です!入力データの最小限の例を提供してください(データの構造を理解するため)。 500の変数はデータフレーム列に格納されていますか?データは1週間に及ぶか?そうでない場合、どのようにデータを区別しますか?毎週月曜日のデータから1週間monday? –

+0

元の質問を編集して、データ入力の小さなサンプルを追加しました。私はそれが助けて欲しい! –

関連する問題