2013-10-29 193 views
1

我正在嘗試做一個新的向量值的組合循環與簡單的減法函數。R嵌套標準循環

我的數據: 37個日期,每個日期有48個觀測值,其中24個觀測值被指定爲'治療',24個是'對照'(因此每天有24對,每對有一對治療一個用作控制)。

我想創建一個新的變量,即控制和治療流量值之間的差異(從而隔離每個對集合內控制流量的治療流量的定量影響),但保留所有其他元數據(日期,配對等)。

這是我的數據有一天是這樣的:

date collar setID withinset CH4_mg.m2.h CO2_mg.m2.h 
6/20/2013 1 1 t 0.557704455 2930.10525 
6/20/2013 2 1 c 0.49434559 2823.564824 
6/20/2013 1S 2 c 2.205589818 2014.835162 
6/20/2013 2S 2 t 2.854174288 1996.614314 
6/20/2013 3 3 c 4.548922035 1818.766532 
6/20/2013 4 3 t 2.352010011 1575.160171 
6/20/2013 3S 4 c 1.022583517 1289.122553 
6/20/2013 4S 4 t 4.377283389 2888.582123 
6/20/2013 5 5 t 1.340228189 2636.685313 
6/20/2013 6 5 c 1.1954218 1782.670702 
6/20/2013 5N 6 c 4.217147165 1631.184251 
6/20/2013 6N 6 t 1.836410187 1031.5654 
6/20/2013 7 7 t 2.051102645 2609.285292 
6/20/2013 8 7 c 1.96837465 2454.56188 
6/20/2013 7N 8 c 3.66876257 2253.766863 
6/20/2013 8N 8 t 3.460709848 2853.823753 
6/20/2013 9 9 t 1.084707894 771.0890746 
6/20/2013 10 9 c 1.915678246 857.8528567 
6/20/2013 9S 10 c 3.555408983 569.5288078 
6/20/2013 10S 10 t 3.401276615 588.6532344 
6/20/2013 11 11 c 2.970877855 1324.872897 
6/20/2013 12 11 t 2.028830249 956.9233078 
6/20/2013 11S 12 t 8.063764267 1516.712685 
6/20/2013 12S 12 c 4.160007577 986.7419756 
6/20/2013 13 13 c 8.351615925 1484.538885 
6/20/2013 14 13 t 7.682825572 1573.40649 
6/20/2013 13N 14 c 6.688854043 1400.82208 
6/20/2013 14N 14 t 4.426522661 985.5632563 
6/20/2013 15 15 c 2.240328624 467.566316 
6/20/2013 16 15 t 2.395533405 470.3854269 
6/20/2013 15N 16 c 3.145509032 1053.025448 
6/20/2013 16N 16 t 3.989964648 1602.760702 
6/20/2013 17 17 t 3.117849324 656.6618375 
6/20/2013 18 17 c 3.719289098 575.5902064 
6/20/2013 17S 18 t 2.75248536 914.3974523 
6/20/2013 18S 18 c 3.253130586 906.1170518 
6/20/2013 19 19 c 2.068481806 465.0783511 
6/20/2013 20 19 t 6.696415968 1362.594187 
6/20/2013 19N 20 t 3.25099946 437.389186 
6/20/2013 20N 20 c 2.923361538 504.803891 
6/20/2013 21N 21 t 5.704969796 1190.943268 
6/20/2013 22N 21 c 7.014650089 1550.961323 
6/20/2013 23S 22 c 6.277550864 1408.528849 
6/20/2013 24S 22 t 8.3399388 1573.475572 
6/20/2013 21 23 c 7.722659069 1467.822676 
6/20/2013 22 23 t 12.51091848 1276.049909 
6/20/2013 23 24 t 10.81073531 2052.516537 
6/20/2013 24 24 c 0.797904749 884.0794505 

我需要一組兩個CO2通量(CO2_mg.m2.h)和CH4通量碼(CH4_mg.m2.h ),但一旦我得到基本骨架代碼的工作,它應該很容易重複。

這是我嘗試對CH4代碼:

t_minus_c <- rep(0,37) # 37 dates 
    for (i in 1:37){ 
    for (j in 1:24){ #24 collar pairs, aka setID 
    {tmc[i] <- ((data$CH4_mg.m2.h[which(data$date==i & data$setID==j & withinset=="t"),]) - 
    (data$CH4_mg.m2.h[which(data$date==i & data$setID==j & withinset=="c"),]))} 
    } 
} 

我需要一組代碼兩種CO2通量(CO2_mg.m2.h)和CH4通量(CH4_mg.m2.h)分開。

我知道這是笨重的,任何幫助將不勝感激。先謝謝你。

回答

0

您可以考慮使用data.table而不是data.frame。它可以很容易地和優雅地完成這種工作。

假設您的數據是data.frameDT

require(data.table) 
DT <- data.table(DF) 
setkey(DT, date, setID, withinset) #order data by date, then setID and then withinset 
TMP <- DT[, list(CH4 = diff(CH4_mg.m2.h), CO2 = diff(CO2_mg.m2.h)), by=list(date, setID)] 
TMP 

的輸出將是

  date setID   CH4   CO2 
1: 6/20/2013  1 0.06335886 106.540426 
2: 6/20/2013  2 0.64858447 -18.220848 
3: 6/20/2013  3 -2.19691202 -243.606361 
4: 6/20/2013  4 3.35469987 1599.459570 
5: 6/20/2013  5 0.14480639 854.014611 
6: 6/20/2013  6 -2.38073698 -599.618851 
7: 6/20/2013  7 0.08272799 154.723412 
8: 6/20/2013  8 -0.20805272 600.056890 
9: 6/20/2013  9 -0.83097035 -86.763782 
10: 6/20/2013 10 -0.15413237 19.124427 
11: 6/20/2013 11 -0.94204761 -367.949589 
12: 6/20/2013 12 3.90375669 529.970709 
13: 6/20/2013 13 -0.66879035 88.867605 
14: 6/20/2013 14 -2.26233138 -415.258824 
15: 6/20/2013 15 0.15520478 2.819111 
16: 6/20/2013 16 0.84445562 549.735254 
17: 6/20/2013 17 -0.60143977 81.071631 
18: 6/20/2013 18 -0.50064523 8.280401 
19: 6/20/2013 19 4.62793416 897.515836 
20: 6/20/2013 20 0.32763792 -67.414705 
21: 6/20/2013 21 -1.30968029 -360.018055 
22: 6/20/2013 22 2.06238794 164.946723 
23: 6/20/2013 23 4.78825941 -191.772767 
24: 6/20/2013 24 10.01283056 1168.437087 
     date setID   CH4   CO2 

簡而言之爲data.table DTDT[i,j,by]選擇來自DT行滿足條件i,創建使用列表j對於每個組中指定的計算的列由分組提供由列表by提供的變量。這是data.table如何工作的非常近似的解釋。爲了更好地理解,您應該閱讀data.table手冊。

+0

僅供參考,共識似乎是基於使用data.table在答案基於反對:http://chat.stackoverflow.com/rooms/106/conversation/data-table-tagging – Frank

+0

@Frank好的。刪除了標籤。 –

+0

非常感謝!這也適用於我。 – user2930281