slice-methods {aqp} | R Documentation |
Slicing of SoilProfilecollection Objects
# method for SoilProfileCollection objects slice(object, fm, top.down=TRUE, just.the.data=FALSE, strict=TRUE)
object |
a SoilProfileCollection |
fm |
A formula: either ‘integer.vector ~ var1 + var2 + var3’ where named variables are sliced according to ‘integer.vector’ OR where all variables are sliced accordin to ‘integer.vector’ ‘integer.vector ~.’. |
top.down |
Logical, should slices be defined from the top-down? The default is usually what you want. |
just.the.data |
Logical, return just the sliced data or a new SoilProfileCollection object. |
strict |
Logical, should the horizonation be strictly checked for self-consistency? |
Either a new SoilProfileCollection with data sliced according to fm
, or a data.frame
.
Typical usage, where input is a SoilProfileCollection
.
slab()
and slice()
are much faster and require less memory if input data are either numeric or character.
D.E. Beaudette
D.E. Beaudette, P. Roudier, A.T. O'Geen, Algorithms for quantitative pedology: A toolkit for soil scientists, Computers & Geosciences, Volume 52, March 2013, Pages 258-268, 10.1016/j.cageo.2012.10.020.
# simulate some data, IDs are 1:20
library(plyr)
d <- ldply(1:20, random_profile)
# init SoilProfilecollection object
depths(d) <- id ~ top + bottom
head(horizons(d))
## id top bottom name p1 p2 p3 p4 p5
## 1 1 0 30 H1 5.142093 3.3517600 -22.266830 1.4649842 -1.016576
## 2 1 30 35 H2 13.100598 12.7448611 -24.952041 9.5539154 12.003094
## 3 1 35 44 H3 15.946593 8.3350776 -25.641457 11.4474244 2.294511
## 4 1 44 63 H4 18.708502 8.4855170 -19.709556 4.5354839 -2.787059
## 5 1 63 76 H5 8.036378 1.9806831 -27.068924 0.1018302 -2.203705
## 6 2 0 21 H1 -5.379072 0.9234663 -9.027768 -14.3507498 11.481567
# generate single slice at 10 cm
# output is a SoilProfilecollection object
s <- slice(d, 10 ~ name + p1 + p2 + p3)
# generate single slice at 10 cm, output data.frame
s <- slice(d, 10 ~ name + p1 + p2 + p3, just.the.data=TRUE)
# generate integer slices from 0 - 25 cm
s <- slice(d, 0:25 ~ name + p1 + p2 + p3)
plot(s)
# generate slices from 0 - 10 cm, for all variables
s <- slice(d, 0:10 ~ .)
print(s)
## Object of class SoilProfileCollection
## Number of profiles: 20
## Depth range: 11-11 cm
##
## Horizon attributes:
## id top bottom name p1 p2 p3 p4 p5
## 1 1 0 1 H1 5.142093 3.35176 -22.26683 1.464984 -1.016576
## 21 1 1 2 H1 5.142093 3.35176 -22.26683 1.464984 -1.016576
## 41 1 2 3 H1 5.142093 3.35176 -22.26683 1.464984 -1.016576
## 61 1 3 4 H1 5.142093 3.35176 -22.26683 1.464984 -1.016576
## 81 1 4 5 H1 5.142093 3.35176 -22.26683 1.464984 -1.016576
## 101 1 5 6 H1 5.142093 3.35176 -22.26683 1.464984 -1.016576
## .pctMissing
## 1 0
## 21 0
## 41 0
## 61 0
## 81 0
## 101 0
##
## Sampling site attributes:
## id
## 1 1
## 2 2
## 3 3
## 4 4
## 5 5
## 6 6
# note that pct missing is computed for each slice,
# if all vars are missing, then NA is returned
d$p1[1:10] <- NA
s <- slice(d, 10 ~ ., just.the.data=TRUE)
print(s)
## id top bottom name p1 p2 p3 p4
## 1 1 10 11 H1 NA 3.3517600 -22.266830 1.4649842
## 2 2 10 11 H1 NA 0.9234663 -9.027768 -14.3507498
## 3 3 10 11 H2 NA -1.6217331 2.603666 8.5496906
## 4 4 10 11 H1 -5.715144 4.2685432 -10.474706 5.6225635
## 5 5 10 11 H1 -5.190446 10.7353658 -4.143216 -13.9901938
## 6 6 10 11 H2 -3.848700 12.2355311 4.844001 -13.0877335
## 7 7 10 11 H1 -12.402731 -3.3249847 1.934476 -6.1899736
## 8 8 10 11 H1 11.754411 -2.1168784 -11.163787 5.9234289
## 9 9 10 11 H1 -12.764023 3.0791096 -7.224875 -4.4342155
## 10 10 10 11 H1 13.725299 -11.8451862 0.827700 0.7629475
## 11 11 10 11 H1 -3.861798 -8.7459027 -1.668516 6.0454818
## 12 12 10 11 H1 7.647671 -19.1208766 11.525158 -7.7055616
## 13 13 10 11 H2 31.417017 11.8167741 23.455177 -4.2725268
## 14 14 10 11 H1 -5.089040 -8.9112453 2.339322 9.6548543
## 15 15 10 11 H1 -4.199460 -1.6876502 8.615376 11.5925719
## 16 16 10 11 H1 -2.714681 2.5933475 -9.716158 -10.0557502
## 17 17 10 11 H1 12.204258 -8.2840986 -2.322851 -6.3291198
## 18 18 10 11 H1 -4.257575 -12.1396421 -3.965111 -0.2556436
## 19 19 10 11 H1 6.769150 -6.5360525 6.186774 -8.0190763
## 20 20 10 11 H1 12.885120 -8.1299763 -2.648393 -5.7267896
## p5 .pctMissing
## 1 -1.0165758 0.1666667
## 2 11.4815672 0.1666667
## 3 11.3117352 0.1666667
## 4 -12.8972069 0.0000000
## 5 -0.5391382 0.0000000
## 6 24.9515130 0.0000000
## 7 -5.8713995 0.0000000
## 8 -2.2293698 0.0000000
## 9 -0.7065403 0.0000000
## 10 -9.9859863 0.0000000
## 11 9.0911617 0.0000000
## 12 4.1954167 0.0000000
## 13 -9.8915766 0.0000000
## 14 7.9471756 0.0000000
## 15 -1.3530898 0.0000000
## 16 -2.3775653 0.0000000
## 17 -6.5247096 0.0000000
## 18 16.0435302 0.0000000
## 19 -5.3420121 0.0000000
## 20 -3.0086987 0.0000000
##
## check sliced data
##
# test that mean of 1 cm slices property is equal to the
# hz-thickness weighted mean value of that property
data(sp1)
depths(sp1) <- id ~ top + bottom
# get the first profile
sp1.sub <- sp1[which(profile_id(sp1) == 'P009'), ]
# compute hz-thickness wt. mean
hz.wt.mean <- with(horizons(sp1.sub),
sum((bottom - top) * prop) / sum(bottom - top)
)
# hopefully the same value, calculated via slice()
s <- slice(sp1.sub, 0:max(sp1.sub) ~ prop)
hz.slice.mean <- mean(s$prop, na.rm=TRUE)
# same?
if(!all.equal(hz.slice.mean, hz.wt.mean))
stop('there is a bug in slice() !!!')