我有氣象數據表,這種想法是運行基於應用的變化曲線的數據幀
一)氣象數據
B)天氣+ 1SD
c)氣象模型 - 1sd
想出一些類型的置信區間。所以在這裏我每天都有兩個城市的臨時工,然後是每個月按標準開發人員的等價表格。我想要做的是編寫一個函數,通過對每個值應用相關的每月st.devs來轉換數據框。就下文而言,我想分別爲博伊西和愛達荷瀑布每年11月的價值增加9.07度和9.37度......然後分別爲博伊西和愛達荷州的所有12月數值分別增加9.15和11.03度的值。
我知道我可以用一些中間步驟以「凌亂」的方式做到這一點,創建一些列,然後最終清理它們。但爲了學習的興趣,我想了解如何執行更優雅的解決方案。
df <- structure(list(Date = c("2014-11-01", "2014-11-02", "2014-11-03",
"2014-11-04", "2014-11-05", "2014-11-06", "2014-11-07", "2014-11-08",
"2014-11-09", "2014-11-10", "2014-11-11", "2014-11-12", "2014-11-13",
"2014-11-14", "2014-11-15", "2014-11-16", "2014-11-17", "2014-11-18",
"2014-11-19", "2014-11-20", "2014-11-21", "2014-11-22", "2014-11-23",
"2014-11-24", "2014-11-25", "2014-11-26", "2014-11-27", "2014-11-28",
"2014-11-29", "2014-11-30", "2014-12-01", "2014-12-02", "2014-12-03",
"2014-12-04", "2014-12-05", "2014-12-06", "2014-12-07", "2014-12-08",
"2014-12-09", "2014-12-10", "2014-12-11", "2014-12-12", "2014-12-13",
"2014-12-14", "2014-12-15", "2014-12-16", "2014-12-17", "2014-12-18",
"2014-12-19", "2014-12-20", "2014-12-21", "2014-12-22", "2014-12-23",
"2014-12-24", "2014-12-25", "2014-12-26", "2014-12-27", "2014-12-28",
"2014-12-29", "2014-12-30"), BOISE = c(44.5, 42.5, 43.5, 47.5,
55, 57.5, 49.5, 47.5, 45, 38, 31, 23.5, 24, 21.5, 11.5, 13, 13,
13, 16, 22, 32, 42, 37, 38, 46.5, 48.5, 49.5, 52.5, 42, 26, 31.5,
33, 40, 48.5, 40, 44, 43.5, 42, 42.5, 46, 57, 51, 39.5, 34, 36.5,
39, 36.5, 40.5, 40.5, 40, 43.5, 39.5, 35.5, 33, 32, 29, 27, 31,
27, 20.5699996948242), `IDAHO FALLS` = c(54.5, 36, 34.5, 35.5,
41, 41.5, 47, 39, 45.5, 36, 15, 13, 14, 26, 4.5, 2.5, 8, 11,
28, 27, 27, 35.5, 31.5, 33, 39, 43, 45.5, 46, 42.5, 28.5, 27,
34, 35.5, 42, 36.5, 42.5, 35, 36, 34.5, 36.5, 42.5, 47, 39, 28,
23.5, 31, 22.5, 24.5, 34.5, 35, 38.5, 34, 27.5, 31.5, 24.5, 8.5,
15, 19, 10.5, -3.46000003814697)), class = "data.frame", .Names = c("Date",
"BOISE", "IDAHO FALLS"), row.names = c(NA, -60L))
sd_matrix <- structure(list(month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
), BOISE = c(7.90623167260698, 6.46123050256436, 6.38106936624632,
7.22283114115187, 7.76515042234502, 8.10445388054925, 5.65058663778116,
6.18033208264487, 7.34160028246709, 7.48784870009556, 9.07481352622016,
9.15757443706943), `IDAHO FALLS` = c(10.4267588417941, 9.89036971863809,
7.99156512696757, 6.84627542213131, 6.6696338642145, 6.823026513784,
4.31982292105468, 4.63179196395735, 6.38702016727256, 7.31441201561822,
9.37466284053354, 11.0316440728702)), class = "data.frame", row.names = c(NA,
-12L), .Names = c("month", "BOISE", "IDAHO FALLS"))
下面是一些哈克代碼,提供正確的結果在這種特定情況下,但沒有提供的東西像變量名稱和大小,我將不得不面對 -
df$month <- month(df$Date)
df <- inner_join(df, sd_matrix, by="month")
df$BOISE.x <- df$BOISE.x + df$BOISE.y
df$`IDAHO FALLS.x` <- df$`IDAHO FALLS.x` + df$`IDAHO FALLS.y`
df <- df %>%
select(Date, BOISE.x, `IDAHO FALLS.x`)
names(df) <- c("Date,", "Boise", "Idaho Falls")
'我可以用「凌亂」的方式做到這一點......'請儘量分享你的代碼。 – zx8754
我編輯了原件以包含此內容。 – jsg51483