私はあなたが開始と終了の間のすべての時間を見つけるために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.table
(dt_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
は、DayOfWeekStr
がdt_all_hours
が同じために24時間に1が含ま、locationid
(locationid
)が新しく、Applicant
の一意の組合せごとにstarthour
とendhour
間の時間が含まれていますApplicant
,DayOfWeekStr
,locationid
のユニークな組み合わせ。 dt_open_hours
とdt_all_hours
のマージは、Applicant
,DayOfWeekStr
,locationid
およびhour
(locationid
は新規)で行われます。
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"]
終了時間は何ですか? –
の質問で混乱します。例えば、開始時間は14、終了時間は22になるので、食品トラックは午後2時から午後10時までオープンします。 –