简体   繁体   中英

R nested criteria loops

I am attempting to make a new vector of values that combines a loop with a simple subtraction function.

My data: 37 dates, with 48 observations per date, of which 24 observations are specified as 'treatment', and 24 are 'control' (so there are 24 pairs each day, and within each pair one was treated and one serves as the control).

I want to make a new variable that is the difference between the control and treatment flux values (thus isolating the quantitative effect of the treatment flux OVER the control flux within each pair set), but maintaining all of the other metadata (date, pair set, etc.).

This is what one day of my data looks like:

    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

I need to make a set of code for both CO2 flux (CO2_mg.m2.h) and CH4 flux (CH4_mg.m2.h) separately, but once I get the base skeleton code working, it should be easy to duplicate.

This is my attempted code for 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"),]))}
    }
}

I need to make a set of code for both CO2 flux (CO2_mg.m2.h) and CH4 flux (CH4_mg.m2.h) separately.

I know it's clunky, and any help would be greatly appreciated. Thank you in advance.

You can consider using data.table instead of data.frame . It can get this kind of work done quite easily and elgantly..

Assuming your data is 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

The output will be

         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

Simply put for data.table DT , DT[i,j,by] selects rows from DT which satisfy condition i , creates columns using calculations specified in list j for every group provided by grouping variables provided by list by . This is very approximate explanation of how data.table works. For better understanding, you should read data.table manual.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM