我正在嘗試做一個新的向量值的組合循環與簡單的減法函數。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)分開。
我知道這是笨重的,任何幫助將不勝感激。先謝謝你。
僅供參考,共識似乎是基於使用data.table在答案基於反對:http://chat.stackoverflow.com/rooms/106/conversation/data-table-tagging – Frank
@Frank好的。刪除了標籤。 –
非常感謝!這也適用於我。 – user2930281