Convenience function to reorganize soil layer time series data from layer_output list entry produced with run_LWFB90. The data is tansformed to a wide format, by casting the variables with the layer number using data.table's dcast-function.

extract_layer_output(
  x,
  layers = NULL,
  value_vars = NULL,
  layer_index_name = "nl",
  sep = ""
)

Arguments

x

Data.frame or data.table with layer data organized in rows and identified by a layer index column named layer_index_nm.

layers

Integer vector to select a subset of layers. If not supplied, values from all layers will be returned.

value_vars

Character vector containing names of value-variables to be extracted from x. If not supplied, value_vars will be guessed.

layer_index_name

Column containing layer index. Defaults to 'nl' as in layer_output.

sep

Separation character for constructig names from variable name and layer index.

Value

A data.table with the layers' values of the variables organized in columns with the names being made up of the variable name and the layer index.

Examples

# create a data.frame with monthly values
# identifiers: layer number, yr and mo
df <- expand.grid(nl = 1:5,
                  yr = 2002,
                  mo = 1:12)
df
#>    nl   yr mo
#> 1   1 2002  1
#> 2   2 2002  1
#> 3   3 2002  1
#> 4   4 2002  1
#> 5   5 2002  1
#> 6   1 2002  2
#> 7   2 2002  2
#> 8   3 2002  2
#> 9   4 2002  2
#> 10  5 2002  2
#> 11  1 2002  3
#> 12  2 2002  3
#> 13  3 2002  3
#> 14  4 2002  3
#> 15  5 2002  3
#> 16  1 2002  4
#> 17  2 2002  4
#> 18  3 2002  4
#> 19  4 2002  4
#> 20  5 2002  4
#> 21  1 2002  5
#> 22  2 2002  5
#> 23  3 2002  5
#> 24  4 2002  5
#> 25  5 2002  5
#> 26  1 2002  6
#> 27  2 2002  6
#> 28  3 2002  6
#> 29  4 2002  6
#> 30  5 2002  6
#> 31  1 2002  7
#> 32  2 2002  7
#> 33  3 2002  7
#> 34  4 2002  7
#> 35  5 2002  7
#> 36  1 2002  8
#> 37  2 2002  8
#> 38  3 2002  8
#> 39  4 2002  8
#> 40  5 2002  8
#> 41  1 2002  9
#> 42  2 2002  9
#> 43  3 2002  9
#> 44  4 2002  9
#> 45  5 2002  9
#> 46  1 2002 10
#> 47  2 2002 10
#> 48  3 2002 10
#> 49  4 2002 10
#> 50  5 2002 10
#> 51  1 2002 11
#> 52  2 2002 11
#> 53  3 2002 11
#> 54  4 2002 11
#> 55  5 2002 11
#> 56  1 2002 12
#> 57  2 2002 12
#> 58  3 2002 12
#> 59  4 2002 12
#> 60  5 2002 12

#add a value variable
df$var <- runif(nrow(df), -1,0)

extract_layer_output(df)
#> Key: <yr, mo>
#>        yr    mo       var1       var2        var3        var4       var5
#>     <num> <int>      <num>      <num>       <num>       <num>      <num>
#>  1:  2002     1 -0.7575457 -0.3728421 -0.83158540 -0.55521037 -0.8353248
#>  2:  2002     2 -0.8817232 -0.6325356 -0.05344109 -0.12458924 -0.7399411
#>  3:  2002     3 -0.3427016 -0.8781354 -0.68115196 -0.37693897 -0.5530026
#>  4:  2002     4 -0.2917066 -0.1099710 -0.89569350 -0.36958828 -0.1584411
#>  5:  2002     5 -0.6357376 -0.3770772 -0.53925249 -0.83658772 -0.4861331
#>  6:  2002     6 -0.8567336 -0.3497013 -0.84470547 -0.49169014 -0.9141296
#>  7:  2002     7 -0.4969733 -0.9614281 -0.17596712 -0.71401443 -0.7532491
#>  8:  2002     8 -0.6020064 -0.3679071 -0.05142115 -0.65988076 -0.3726340
#>  9:  2002     9 -0.1750846 -0.8548846 -0.03286087 -0.91396101 -0.7595388
#> 10:  2002    10 -0.3611163 -0.4966049 -0.74947674 -0.02096623 -0.3434204
#> 11:  2002    11 -0.9101511 -0.3694571 -0.12323234 -0.06971846 -0.5805141
#> 12:  2002    12 -0.5044401 -0.6472953 -0.75126513 -0.04127210 -0.6400399

# add more variables
df$var1 <- runif(nrow(df), 1,2)
df$var2 <- runif(nrow(df), 2,3)

# extract specific layers
extract_layer_output(df,layers = 2:4, sep = "_layer")
#> Key: <yr, mo>
#>        yr    mo var_layer2  var_layer3  var_layer4 var1_layer2 var1_layer3
#>     <num> <int>      <num>       <num>       <num>       <num>       <num>
#>  1:  2002     1 -0.3728421 -0.83158540 -0.55521037    1.982191    1.316116
#>  2:  2002     2 -0.6325356 -0.05344109 -0.12458924    1.405270    1.924520
#>  3:  2002     3 -0.8781354 -0.68115196 -0.37693897    1.269695    1.906492
#>  4:  2002     4 -0.1099710 -0.89569350 -0.36958828    1.488289    1.091970
#>  5:  2002     5 -0.3770772 -0.53925249 -0.83658772    1.051062    1.737795
#>  6:  2002     6 -0.3497013 -0.84470547 -0.49169014    1.002477    1.569741
#>  7:  2002     7 -0.9614281 -0.17596712 -0.71401443    1.495835    1.696489
#>  8:  2002     8 -0.3679071 -0.05142115 -0.65988076    1.243791    1.700768
#>  9:  2002     9 -0.8548846 -0.03286087 -0.91396101    1.255749    1.039941
#> 10:  2002    10 -0.4966049 -0.74947674 -0.02096623    1.552319    1.431522
#> 11:  2002    11 -0.3694571 -0.12323234 -0.06971846    1.251578    1.814231
#> 12:  2002    12 -0.6472953 -0.75126513 -0.04127210    1.628450    1.566586
#>     var1_layer4 var2_layer2 var2_layer3 var2_layer4
#>           <num>       <num>       <num>       <num>
#>  1:    1.588247    2.315731    2.356315    2.635668
#>  2:    1.880309    2.816188    2.571561    2.338220
#>  3:    1.719318    2.545915    2.677988    2.962416
#>  4:    1.936429    2.947317    2.690719    2.111790
#>  5:    1.429432    2.038526    2.373411    2.650485
#>  6:    1.286187    2.647644    2.097978    2.983512
#>  7:    1.770643    2.079619    2.099010    2.179128
#>  8:    1.616499    2.432931    2.774802    2.137970
#>  9:    1.159275    2.760893    2.151496    2.056559
#> 10:    1.401598    2.293759    2.394607    2.546368
#> 11:    1.001646    2.107332    2.934998    2.466505
#> 12:    1.417966    2.072994    2.739590    2.896378

#extract specific variables
extract_layer_output(df, layers = 2:4, value_vars = c("var1", "var2"), sep = "_layer")
#> Key: <yr, mo>
#>        yr    mo var1_layer2 var1_layer3 var1_layer4 var2_layer2 var2_layer3
#>     <num> <int>       <num>       <num>       <num>       <num>       <num>
#>  1:  2002     1    1.982191    1.316116    1.588247    2.315731    2.356315
#>  2:  2002     2    1.405270    1.924520    1.880309    2.816188    2.571561
#>  3:  2002     3    1.269695    1.906492    1.719318    2.545915    2.677988
#>  4:  2002     4    1.488289    1.091970    1.936429    2.947317    2.690719
#>  5:  2002     5    1.051062    1.737795    1.429432    2.038526    2.373411
#>  6:  2002     6    1.002477    1.569741    1.286187    2.647644    2.097978
#>  7:  2002     7    1.495835    1.696489    1.770643    2.079619    2.099010
#>  8:  2002     8    1.243791    1.700768    1.616499    2.432931    2.774802
#>  9:  2002     9    1.255749    1.039941    1.159275    2.760893    2.151496
#> 10:  2002    10    1.552319    1.431522    1.401598    2.293759    2.394607
#> 11:  2002    11    1.251578    1.814231    1.001646    2.107332    2.934998
#> 12:  2002    12    1.628450    1.566586    1.417966    2.072994    2.739590
#>     var2_layer4
#>           <num>
#>  1:    2.635668
#>  2:    2.338220
#>  3:    2.962416
#>  4:    2.111790
#>  5:    2.650485
#>  6:    2.983512
#>  7:    2.179128
#>  8:    2.137970
#>  9:    2.056559
#> 10:    2.546368
#> 11:    2.466505
#> 12:    2.896378