Skip to content

Commit 9b91ad2

Browse files
authored
Merge pull request #48 from ncss-tech/extractSubgroupSMR
extractSMR updates
2 parents dff0043 + d2ab06e commit 9b91ad2

File tree

2 files changed

+57
-46
lines changed

2 files changed

+57
-46
lines changed

R/extractSMR.R

+46-37
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,59 @@
1212
#' @examples
1313
#' extractSMR(c("aquic haploxeralfs", "typic epiaqualfs", "humic inceptic eutroperox"))
1414
extractSMR <- function(taxon, as.is = FALSE, droplevels = FALSE, ordered = TRUE) {
15-
res <- vapply(taxon, .extractSMR, character(1))
15+
16+
.get_SMR_element_connotation <- function() {
17+
data.frame(element = c("ids",
18+
"per", "aqu", "torr", "ud", "ust", "xer", "sapr", "hem", "fibr", "wass",
19+
"torri", "ud", "ust", "xer", "aqu",
20+
"ud", "ust", "xer"),
21+
level = c("order",
22+
"suborder", "suborder", "suborder", "suborder", "suborder", "suborder", "suborder", "suborder", "suborder", "suborder",
23+
"greatgroup", "greatgroup", "greatgroup", "greatgroup", "greatgroup",
24+
"subgroup", "subgroup", "subgroup"),
25+
connotation = c("aridic (torric)",
26+
"perudic", "aquic", "aridic (torric)", "udic", "ustic", "xeric", "aquic", "aquic", "aquic", "peraquic",
27+
"aridic (torric)", "udic", "ustic", "xeric", "aquic",
28+
"udic", "ustic", "xeric"),
29+
stringsAsFactors = FALSE)
30+
}
31+
32+
# get SMR formative element connotation LUT
33+
co <- .get_SMR_element_connotation()
34+
35+
res <- vapply(taxon, function(taxon) {
36+
37+
# extract formative elements
38+
el <- FormativeElements(taxon)
39+
40+
# determine taxon level and position
41+
el$defs$hierarchy <- level_hierarchy(el$defs$level)
42+
th <- min(el$defs$hierarchy, na.rm = TRUE)
43+
44+
# only consider SMR formative elements at or below taxon level
45+
el$defs <- el$defs[grepl(paste0(co$element, collapse = "|"), el$defs$element) & th <= el$defs$level, ]
46+
maxlevel <- suppressWarnings(max(el$defs$hierarchy, na.rm = TRUE))
47+
el$defs <- el$defs[el$defs$hierarchy == maxlevel, ]
48+
49+
# THEN get highest level taxon SMR connotation
50+
co2 <- co[!is.na(pmatch(co$element, el$defs$element, duplicates.ok = TRUE)) &
51+
co$level %in% el$defs$level &
52+
co$level == maxlevel, ]
53+
nrx <- nrow(co2)
54+
if (nrx == 1) {
55+
co2$connotation
56+
} else NA_character_
57+
}, character(1))
58+
1659
if (as.is) {
1760
return(res)
1861
}
62+
1963
res <- factor(res, levels = SoilMoistureRegimeLevels(as.is = TRUE), ordered = ordered)
2064
if (droplevels) {
2165
return(droplevels(res))
2266
}
67+
2368
names(res) <- taxon
2469
res
2570
}
26-
27-
.extractSMR <- function(taxon) {
28-
29-
# extract formative elements
30-
el <- FormativeElements(taxon)
31-
32-
# determine taxon level and position
33-
el$defs$hierarchy <- level_hierarchy(el$defs$level)
34-
th <- min(el$defs$hierarchy, na.rm = TRUE)
35-
36-
# get SMR formative element connotation LUT
37-
co <- .get_SMR_element_connotation()
38-
39-
# only consider SMR formative elements at or below taxon level
40-
el$defs <- el$defs[el$defs$element %in% co$element & th <= el$defs$level,]
41-
42-
# THEN get highest level taxon SMR connotation
43-
co <- co[co$element %in% el$defs$element &
44-
co$level %in% el$defs$level &
45-
co$level == suppressWarnings(max(el$defs$hierarchy, na.rm = TRUE)), ]
46-
nrx <- nrow(co)
47-
if (nrx == 1) {
48-
# todo handle per+aqu and per+ud
49-
co$connotation
50-
} else NA_character_
51-
}
52-
53-
.get_SMR_element_connotation <- function() {
54-
# x <- get_ST_formative_elements()
55-
# x[grepl("SMR|wetness", x$connotation) & x$level != "subgroup",][c("element","level")]
56-
## NB: currently there is no formative element connotation for "peraquic" soils
57-
data.frame(element = c("per", "ids", "aqu", "torr", "ud", "ust", "xer", "torri", "ud", "ust", "xer", "aqu"),
58-
level = c("suborder", "order", "suborder", "suborder", "suborder", "suborder", "suborder", "greatgroup", "greatgroup", "greatgroup", "greatgroup", "greatgroup"),
59-
connotation = c("perudic", "aridic (torric)", "aquic", "aridic (torric)", "udic", "ustic", "xeric", "aridic (torric)", "udic", "ustic", "xeric", "aquic"),
60-
stringsAsFactors = FALSE)
61-
}

tests/testthat/test-extractSMR.R

+11-9
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ test_that("extractSMR works", {
1616
`aquisalids` = "aridic (torric)",
1717
`aquiturbels` = "aquic"
1818
),
19-
levels = c(
20-
"aridic (torric)",
21-
"ustic",
22-
"xeric",
23-
"udic",
24-
"perudic",
25-
"aquic",
26-
"peraquic"
27-
),
19+
levels = SoilMoistureRegimeLevels(as.is = TRUE),
2820
ordered = TRUE)
2921
)
22+
23+
expect_equal(extractSMR(c('xerollic glossocryalfs', 'ustic haplocambids')),
24+
factor(
25+
c(
26+
`xerollic glossocryalfs` = "xeric",
27+
`ustic haplocambids` = "aridic (torric)"
28+
),
29+
levels = SoilMoistureRegimeLevels(as.is = TRUE),
30+
ordered = TRUE
31+
))
3032
})

0 commit comments

Comments
 (0)