R嵌套标准循环

时间:2013-10-29 02:16:17

标签: r nested-loops

我正在尝试创建一个新的值向量,它将循环与简单的减法函数结合起来。

我的数据: 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)制作一套代码。

我知道这很笨重,任何帮助都会非常感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以考虑使用data.table代替data.frame。它可以非常容易和优雅地完成这种工作..

假设您的数据为data.frame DT

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手册。