我正試圖分解我以前的問題,並制定了一個計劃,以實現我最終尋找的不同步驟。目前,我正在嘗試循環查找是否爲每個唯一源打開機械系統,如source
列中的第一個表中所示。循環遍歷列的唯一值並創建多列
例如,我給出了以下配置文件,告訴我在四個季節中每個系統的典型工作日是幾小時。請注意,有些來源在一天之內有超過一個時段,因此您可以看到堆棧2重複了2個時段。
什麼我想現在要實現的是,我已經創造了一些樣品的日期,想通過每一個獨特的來源做一個循環,只是說無論是特定的時間系統或關閉基於關於Profile
表中提供的信息。到目前爲止,我所做的是創建如下表與下面的代碼:
和下面的代碼將創建上表:
# create dates table
dates =data.frame(dates=seq(
from=as.POSIXct("2010-1-1 0:00", tz="UTC"),
to=as.POSIXct("2012-12-31 23:00", tz="UTC"),
by="hour"))
# add year month day hour weekday column
dates$year <- format(dates[,1], "%Y") # year
dates$month <- format(dates[,1], "%m") # month
dates$day <- format(dates[,1], "%d") # day
dates$hour <- format(dates[,1], "%H") # hour
dates$weekday <- format(dates[,1], "%a") # weekday
# set system locale for reproducibility
Sys.setlocale(category = "LC_TIME", locale = "en_US.UTF-8")
# calculate season column
d = function(month_day) which(lut$month_day == month_day)
lut <- data.frame(all_dates = as.POSIXct("2012-1-1") + ((0:365) * 3600 * 24),
season = NA)
lut <- within(lut, { month_day = strftime(all_dates, "%b-%d") })
lut[c(d("Jan-01"):d("Mar-15"), d("Nov-08"):d("Dec-31")), "season"] = "winter"
lut[c(d("Mar-16"):d("Apr-30")), "season"] = "spring"
lut[c(d("May-01"):d("Sep-27")), "season"] = "summer"
lut[c(d("Sep-28"):d("Nov-07")), "season"] = "autumn"
rownames(lut) = lut$month_day
dates = within(dates, {
season = lut[strftime(dates, "%b-%d"), "season"]
})
什麼我想現在要做的是在profile
表中的列中的每個唯一值的右側添加列,並基於以下標準對數據集中每個小時開啓或關閉系統進行估計。
我很努力的編程概念,如何做到與多個條件相似的vlookup,並在新列中粘貼值。例如,對於我的樣本數據,循環應創建2個程序,因爲Source
列只有2個唯一源Stack 1
和Stack 2
。棘手的一點是if語句和它需要類似的東西:
作爲一個例子,表2的第一行應該匹配季節列的值與profile
表,並查看該小時是否在期間內在系統啓動時的特定季節。如果它在規定的時間內落入,則說'開',如果在外面只說off
。所以結果應該像下圖的這2個紅色字體列:
values <- unique(profile$Source)
但現在它只是沒有進一步使用for循環。
我只是想知道如果有人可以給我任何建議,我怎麼可以做循環,以創建2個更多的列與表2獨特的來源?
下面是典型週刊「個人資料」的數據,我現在用表:
> dput(profile)
structure(list(`Source no` = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Source = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("Stack 1", "Stack 2"), class = "factor"),
Period = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Day = structure(c(2L,
6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L, 7L, 5L, 1L, 3L, 4L, 2L, 6L,
7L, 5L, 1L, 3L, 4L), .Label = c("Fri", "Mon", "Sat", "Sun",
"Thu", "Tue", "Wed"), class = "factor"), `Spring On` = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 15L,
15L, 15L, 15L, 15L, 15L, 15L), `Spring Off` = c(23L, 23L,
23L, 23L, 23L, 23L, 23L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 18L,
18L, 18L, 18L, 18L, 18L, 18L), `Summer On` = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = "off", class = "factor"), `Summer Off` = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = "off", class = "factor"), `Autumn On` = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = "off", class = "factor"), `Autumn Off` = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = "off", class = "factor"), `Winter On` = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L), .Label = c("0", "off"), class = "factor"),
`Winter Off` = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("23",
"off"), class = "factor")), .Names = c("Source no", "Source",
"Period", "Day", "Spring On", "Spring Off", "Summer On", "Summer Off",
"Autumn On", "Autumn Off", "Winter On", "Winter Off"), class = "data.frame", row.names = c(NA,
-21L))
千恩萬謝
你的設置代碼不起作用。檢查這行代碼'日期= data.frame(日期= seq(as.Date('2010-01-01'),as.Date('2012-12-31'),= =「小時」))' –
道歉我把錯誤的代碼,請看看現在更正的一個,謝謝'日期=數據。frame(dates = seq(from = as.POSIXct(「2010-1-1 0:00」,tz =「UTC」),to = as.POSIXct(「2012-12-31 23:00」,tz =「 UTC「),by =」hour「))' – Achak
這條線是什麼意思? ''如果小時與'季節'欄中的值相同,那麼查看一週中的小時,星期幾,並返回系統是否開機。「' –