diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 327613f7..caab4019 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -34,10 +34,9 @@ jobs: R_REMOTES_NO_ERRORS_FROM_WARNINGS: true RSPM: ${{ matrix.config.rspm }} GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} - + steps: - - uses: actions/checkout@v4 - + - uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2 with: r-version: ${{ matrix.config.r }} @@ -52,16 +51,9 @@ jobs: brew install --cask xquartz - uses: r-lib/actions/setup-r-dependencies@v2 - if: matrix.config.r == '3.6' with: extra-packages: any::rcmdcheck, soiltexture=?ignore-before-r=4.3.0, markovchain=?ignore-before-r=4.0.0, Hmisc=?ignore, knitr=?ignore-before-r=4.0.0, rmarkdown=?ignore-before-r=4.0.0, testthat=?ignore-before-r=4.0.0, needs: check - - - uses: r-lib/actions/setup-r-dependencies@v2 - if: matrix.config.r != '3.6' - with: - extra-packages: any::rcmdcheck, markovchain=?ignore-before-r=4.0.0 - needs: check - name: Install soilDB from r-universe (R-devel only) if: matrix.config.r == 'devel' @@ -70,6 +62,14 @@ jobs: shell: Rscript {0} - uses: r-lib/actions/check-r-package@v2 + if: matrix.config.r != '3.6' + with: + upload-snapshots: true + + - uses: r-lib/actions/check-r-package@v2 + if: matrix.config.r == '3.6' with: + args: 'c("--no-manual", "--as-cran", "--ignore-vignettes", "--no-tests")' + build_args: 'c("--no-manual", "--no-build-vignettes")' upload-snapshots: true diff --git a/R/allocate.R b/R/allocate.R index 037b8305..673d4aef 100644 --- a/R/allocate.R +++ b/R/allocate.R @@ -662,38 +662,32 @@ allocate <- function(..., to = c("FAO Salt Severity", "FAO Black Soil", "ST Diag #' @seealso [texture_to_taxpartsize()], [lookup_taxpartsize()] #' #' @export - #' @examples #' #' h <- data.frame( -#' id = 1, -#' hzname = c("A", "BA", "Bw", "BC", "C"), -#' top = c( 0, 10, 45, 60, 90), -#' bottom = c(10, 45, 60, 90, 150), -#' clay = c(15, 16, 45, 20, 10), -#' sand = c(10, 35, 40, 50, 90), -#' frags = c( 0, 5, 10, 38, 40) -#' ) -#' -#' h <- cbind( -#' h, -#' texcl = ssc_to_texcl(clay = h$clay, sand = h$sand) +#' id = 1, +#' hzname = c("A", "BA", "Bw", "BC", "C"), +#' top = c(0, 10, 45, 60, 90), +#' bottom = c(10, 45, 60, 90, 150), +#' clay = c(15, 16, 45, 20, 10), +#' sand = c(10, 35, 40, 50, 90), +#' frags = c(0, 5, 10, 38, 40) #' ) #' -#' pscs <- data.frame( -#' id = 1, -#' top = 25, -#' bottom = 100 -#' ) +#' h <- cbind(h, +#' texcl = ssc_to_texcl(clay = h$clay, sand = h$sand)) +#' +#' pscs <- data.frame(id = 1, +#' top = 25, +#' bottom = 100) #' -#' h <- cbind( -#' h, -#' taxpartsize = texture_to_taxpartsize( -#' texcl = h$texcl, -#' clay = h$clay, -#' sand = h$sand, -#' fragvoltot = h$frags -#' )) +#' h <- cbind(h, +#' taxpartsize = texture_to_taxpartsize( +#' texcl = h$texcl, +#' clay = h$clay, +#' sand = h$sand, +#' fragvoltot = h$frags +#' )) #' #' depths(h) <- id ~ top + bottom #' @@ -713,7 +707,6 @@ hz_to_taxpartsize <- function(x, y, taxpartsize = "taxpartsize", clay = "clay", # xy <- hz_intersect(x, y, idcol = idcol, depthcols = depthcols) # x_sub <- x[x$rn %in% xy$rn, ] - # check segment_id ---- ## if it exists, overwrite it x_nm <- names(x) @@ -723,7 +716,6 @@ hz_to_taxpartsize <- function(x, y, taxpartsize = "taxpartsize", clay = "clay", y[y_nm == "segment_id"] <- NULL } - # check dissolve_id ---- ## if it exists, overwrite it x_nm <- names(x) @@ -733,7 +725,6 @@ hz_to_taxpartsize <- function(x, y, taxpartsize = "taxpartsize", clay = "clay", y[y_nm == "dissolve_id"] <- NULL } - # standardize inputs ---- vars <- c(idcol, depthcols, clay, taxpartsize) x <- x[vars] @@ -744,26 +735,27 @@ hz_to_taxpartsize <- function(x, y, taxpartsize = "taxpartsize", clay = "clay", y <- y[c(idcol, depthcols)] y <- .standardize_inputs(y, idcol = idcol, depthcols = depthcols)$x - # dissolve on pscs ---- # calculate non-trimmed horizon thickness - x_dis <- x |> - hz_dissolve(by = "taxpartsize", idcol = "idcol", depthcols = c("top", "bot")) |> - transform(thk_o = bot - top) - + x_dis <- transform(hz_dissolve(x, + by = "taxpartsize", + idcol = "idcol", + depthcols = c("top", "bot")), + thk_o = bot - top) # trim depths ---- # calculate trimmed horizon thickness - xy_dis <- x_dis |> - hz_intersect(y, idcol = "idcol", depthcols = c("top", "bot")) |> - transform(thk_t = bot - top) - + xy_dis <- transform(hz_intersect(x_dis, y, + idcol = "idcol", + depthcols = c("top", "bot")), + thk_t = bot - top) # rejoin dissolved pscs to the original horizon table ---- - xy <- hz_intersect(x, xy_dis, idcol = "idcol", depthcols = c("top", "bot")) |> suppressWarnings() + xy <- suppressWarnings(hz_intersect(x, xy_dis, + idcol = "idcol", + depthcols = c("top", "bot"))) x_dis <- NULL xy_dis <- NULL - # aggregate clay values within dissolved pscs ---- top <- NULL @@ -777,7 +769,7 @@ hz_to_taxpartsize <- function(x, y, taxpartsize = "taxpartsize", clay = "clay", list( top = min(top, na.rm = TRUE), bot = max(bot, na.rm = TRUE), - clay_wt = weighted.mean(clay, w = thk_t, na.rm = TRUE), + clay_wt = weighted.mean(clay, w = thk_t, na.rm = TRUE), # sandvf_wt = weighted.mean(sandvf, w = thk_t, na.rm = TRUE), # need to impute frags # frag_wt = weighted.mean(total_frags_pct_nopf, w = thk_t), na.rm = TRUE, @@ -788,21 +780,18 @@ hz_to_taxpartsize <- function(x, y, taxpartsize = "taxpartsize", clay = "clay", data.table::setorder(xy_agg, idcol, top) xy_agg <- as.data.frame(xy_agg) - # find adjacent horizons ---- - xy_lag <- xy_agg |> - hz_lag(idcol = "idcol", depthcols = c("top", "bot")) - + xy_lag <- hz_lag(xy_agg, idcol = "idcol", depthcols = c("top", "bot")) # address special cases of strongly contrasting classes ---- clay_wt_bot.1 <- NULL sandvf_wt_bot.1 <- NULL taxpartsize_bot.1 <- NULL - # still needs special cases for very fine sand - xy_agg <- cbind(xy_agg, xy_lag) |> - within({ + xy_agg <- within( + cbind(xy_agg, xy_lag), + { clay_dif = clay_wt_bot.1 - clay_wt sc = paste0(taxpartsize, " over ", taxpartsize_bot.1) sc = gsub(" over NA$", "", sc) @@ -851,58 +840,58 @@ hz_to_taxpartsize <- function(x, y, taxpartsize = "taxpartsize", clay = "clay", # ) # idx_sc = grepl("over", sc) sc = ifelse(idx_sc, sc, taxpartsize) - }) + } + ) xy_lag <- NULL - # find multiple strongly contrasting ps classes within the control section n_sc <- NULL n_peiid <- NULL - test <- data.table::as.data.table(xy_agg)[, list( - n_sc = sum(idx_sc, na.rm = TRUE), # sum(grepl(" over ", sc), na.rm = TRUE), - n_peiid = length(idx_sc) - ), - by = "idcol" - ] |> - as.data.frame() - + test <- as.data.frame(data.table::as.data.table(xy_agg)[, + list(n_sc = sum(idx_sc, na.rm = TRUE), + # sum(grepl(" over ", sc), na.rm = TRUE), + n_peiid = length(idx_sc)), + by = "idcol"]) # pick the sc pscs with the largest contrast or pscs with the greatest thickness - xy_res <- xy_agg |> - merge(test, by = "idcol", all.x = TRUE, sort = FALSE) |> - transform( + xy_res <- transform( + merge( + xy_agg, + test, + by = "idcol", + all.x = TRUE, + sort = FALSE + ), idx_sc = sc %in% .pscs_sc, # idx_sc = grepl(" over ", sc), idx_c_ov_l = sc %in% c("clayey over fine-loamy") ) - xy_res <- data.table::as.data.table(xy_res)[, list( - pscs1 = sc[n_sc == 0 & n_peiid == 1], - pscs2 = sc[n_sc == 1 & n_peiid > 1 & idx_sc], - pscs3 = sc[which.max(thk_t[n_sc == 0 & n_peiid > 1])], - pscs4 = sc[n_sc == 1 & idx_sc], - pscs5 = sc[which.max(abs(clay_dif[n_sc > 1 & !is.na(sc)]))], - taxpartsizemod = ifelse(max(n_sc) > 1, "aniso", "not used") - ), - by = "idcol" - ] |> - as.data.frame() |> - within({ - # need to add fix for special case of sandy over loamy which requires fine sand percent - taxpartsize = paste(pscs1, pscs3, pscs4, pscs5, sep = "") - taxpartsize = gsub("NA", "", taxpartsize) - pscs1 = NULL - pscs2 = NULL - pscs3 = NULL - pscs4 = NULL - pscs5 = NULL - }) - + xy_res <- within(as.data.frame( + data.table::as.data.table(xy_res)[ , + list( + pscs1 = sc[n_sc == 0 & n_peiid == 1], + pscs2 = sc[n_sc == 1 & n_peiid > 1 & idx_sc], + pscs3 = sc[which.max(thk_t[n_sc == 0 & n_peiid > 1])], + pscs4 = sc[n_sc == 1 & idx_sc], + pscs5 = sc[which.max(abs(clay_dif[n_sc > 1 & !is.na(sc)]))], + taxpartsizemod = ifelse(max(n_sc) > 1, "aniso", "not used") + ), + by = "idcol"]), + { + # need to add fix for special case of sandy over loamy which requires fine sand percent + taxpartsize = paste(pscs1, pscs3, pscs4, pscs5, sep = "") + taxpartsize = gsub("NA", "", taxpartsize) + pscs1 = NULL + pscs2 = NULL + pscs3 = NULL + pscs4 = NULL + pscs5 = NULL + }) # reset inputs xy_res <- .reset_inputs(xy_res, x_conv[1]) - return(xy_res) } diff --git a/R/segment.R b/R/segment.R index 1481cd00..dde1aefd 100644 --- a/R/segment.R +++ b/R/segment.R @@ -113,19 +113,17 @@ hz_segment <- function(object, intervals, trim = TRUE, depthcols = c("top", "bot bot = intervals[-1], stringsAsFactors = FALSE ) + n <- max(nchar(intervals)) - dep$id <- paste0( - formatC(dep$top, width = n, flag = 0), - "-", - formatC(dep$bot, width = n, flag = 0) - ) + dep$id <- paste0(formatC(dep$top, width = n, flag = 0), + "-", + formatC(dep$bot, width = n, flag = 0)) # argument sanity check ---- test_spc <- inherits(object, 'SoilProfileCollection') test_df <- inherits(object, 'data.frame') test_dep <- is.numeric(dep$top) & is.numeric(dep$bot) & all(dep$top < dep$bot) - if (!any(test_spc, test_df)) { stop("the input must be either a SoilProfileCollection or data.frame") } @@ -136,7 +134,6 @@ hz_segment <- function(object, intervals, trim = TRUE, depthcols = c("top", "bot stop("intervals should be numeric and sequential (e.g. c(0, 1, 2, 3) or 0:100)") } - # standardize inputs ---- if (test_spc) { idcol <- idname(object) @@ -149,7 +146,6 @@ hz_segment <- function(object, intervals, trim = TRUE, depthcols = c("top", "bot } names(h)[names(h) %in% depthcols] <- c("top", "bot") - ## TODO: consider using dice() # filter horizons and trim ---- .slice <- function(h, top = NULL, bot = NULL) { @@ -164,7 +160,7 @@ hz_segment <- function(object, intervals, trim = TRUE, depthcols = c("top", "bot # h <- h[(h$bot - h$top) > 0, ] } - # h <- h[!is.na(h$peiid), ] + # h <- h[!is.na(h$peiid), ] return(h) } @@ -174,14 +170,17 @@ hz_segment <- function(object, intervals, trim = TRUE, depthcols = c("top", "bot df_str <- cbind(h[0, ], segment_id = NA_character_[0]) dep$df <- list(df_str)[rep(1, nrow(dep))] # pre-allocate memory faster h <- { - split(dep, dep$id) ->.; + split(dep, dep$id) -> . + lapply(., function(x) { temp <- .slice(h, top = x$top, bot = x$bot) - if (nrow(temp) > 0) x$df[[1]] <- cbind(temp, segment_id = x$id) + if (nrow(temp) > 0) + x$df[[1]] <- cbind(temp, segment_id = x$id) return(x$df[[1]]) - }) ->.; - do.call("rbind", .) ->.; - } + }) -> . + + do.call("rbind", .) -> . + } names(h)[names(h) %in% c("top", "bot")] <- depthcols @@ -216,12 +215,10 @@ hz_segment <- function(object, intervals, trim = TRUE, depthcols = c("top", "bot #' @export #' @rdname hz_segment segment <- function(object, intervals, trim = TRUE, hzdepcols = c("top", "bottom")) { - .Deprecated("segment() will be deprecated and replaced by hz_segment()") + .Deprecated("segment() is deprecated and has been replaced by hz_segment()") hz_segment(object, intervals, trim, depthcols = hzdepcols) } - - #' @title Dissolving horizon boundaries by grouping variables #' #' @description This function dissolves or combines horizons that have a common set of grouping variables. It only combines those horizon records that are sequential (e.g. share a horizon boundary). Thus, it can be used to identify discontinuities in the grouping variables along a profile and their unique depths. It is particularly useful for determining the depth to the top or bottom of horizons with a specific category, and should be simpler than previous methods that require aggregating over profiles. @@ -281,8 +278,6 @@ segment <- function(object, intervals, trim = TRUE, hzdepcols = c("top", "bottom #' test <- hz_dissolve(df, "genhz") #' subset(test, value == "2Bt") #' - - hz_dissolve <- function(object, by, idcol = "id", depthcols = c("top", "bottom"), collapse = FALSE, order = FALSE) { # id = "peiid"; hztop = "hzdept"; hzbot = "hzdepb", collapse = FALSE, order = FALSE @@ -296,29 +291,24 @@ hz_dissolve <- function(object, by, idcol = "id", depthcols = c("top", "bottom") if (!any(test_object)) { stop("the object argument must be a data.frame", call. = FALSE) } - # check that collapse is a logical of length 1 if (!inherits(collapse, "logical") || length(collapse) != 1) { stop("the collapse argument must be logical and a length of one", call. = FALSE) } - # check that by is not NULL if (is.null(by)) stop("the by argument must not be NULL") - # check that "by" are characters or convert if (any(!"character" %in% sapply(object[by], class))) { message("non-character grouping variables are being converted to characters") object[by] <- lapply(object[by], as.character) } - # check that the column names exist within the object .check_names(object, vars = c(idcol = idcol, top = depthcols[1], bot = depthcols[2], by)) - # check if previous dissolve_id exists and overwrite nm <- names(object) idx <- nm == "dissolve_id" @@ -327,7 +317,6 @@ hz_dissolve <- function(object, by, idcol = "id", depthcols = c("top", "bottom") object[idx] <- NULL } - # standardize inputs ---- df_std <- .standardize_inputs(object, idcol = idcol, depthcols = depthcols) df_conversion <- df_std$x_conversion @@ -346,7 +335,6 @@ hz_dissolve <- function(object, by, idcol = "id", depthcols = c("top", "bottom") by <- by_co } - # var thickness ---- var_dep <- lapply(by, function(x) { @@ -381,14 +369,9 @@ hz_dissolve <- function(object, by, idcol = "id", depthcols = c("top", "bottom") # ) # } - # append dissolve_id - n <- c( - var_dep$top, - var_dep$bot - ) |> - nchar() |> - max(na.rm = TRUE) + n <- max(nchar(c(var_dep$top, var_dep$bot)), na.rm = TRUE) + var_dep$dissolve_id <- paste0( var_dep$idcol, "_", @@ -411,9 +394,9 @@ hz_dissolve <- function(object, by, idcol = "id", depthcols = c("top", "bottom") #' @rdname hz_dissolve dissolve_hz <- function(object, by, id = "idcol", hztop = "top", hzbot = "bottom", collapse = FALSE, order = FALSE) { - .Deprecated("dissolve_hz() will be deprecated and replaced by hz_dissolve()") + .Deprecated("dissolve_hz() is deprecated and has been replaced by hz_dissolve()") hz_dissolve(object, by, idcol = id, depthcols = c(hztop, hzbot), collapse, order) - } +} @@ -439,25 +422,20 @@ dissolve_hz <- function(object, by, id = "idcol", hztop = "top", hzbot = "bottom #' @examples #' #' h <- data.frame( -#' id = 1, -#' top = c(0, 25, 44, 46, 50), -#' bottom = c(25, 44, 46, 50, 100), -#' by = c("Yes", "Yes", "No", "No", "Yes"), -#' clay = c(10, 12, 27, 35, 16) +#' id = 1, +#' top = c(0, 25, 44, 46, 50), +#' bottom = c(25, 44, 46, 50, 100), +#' by = c("Yes", "Yes", "No", "No", "Yes"), +#' clay = c(10, 12, 27, 35, 16) #' ) #' -#' h |> hz_dissolve("by") +#' hz_dissolve(h, "by") #' -#' h |> hz_dissolve("by") |> hz_intersect(x = _, y = h) +#' hz_intersect(x = hz_dissolve(h, "by"), y = h) #' -#' h |> -#' hz_dissolve("by") |> -#' hz_intersect(x = h, y = _) |> -#' aggregate(clay ~ dissolve_id, data = _, mean) +#' hi <- hz_intersect(x = h, y = hz_dissolve(h, "by")) +#' aggregate(clay ~ dissolve_id, data = hi, mean) #' - - - hz_intersect <- function(x, y, idcol = "id", depthcols = c("top", "bottom")) { # test inputs ---- @@ -471,7 +449,6 @@ hz_intersect <- function(x, y, idcol = "id", depthcols = c("top", "bottom")) { .check_names(x, c(idcol, depthcols)) .check_names(y, c(idcol, depthcols)) - # check segment_id ---- ## if it exists, overwrite it x_nm <- names(x) @@ -486,7 +463,6 @@ hz_intersect <- function(x, y, idcol = "id", depthcols = c("top", "bottom")) { y[y_nm == "segment_id"] <- NULL } - # standardize inputs ---- x_std <- .standardize_inputs(x, idcol = idcol, depthcols = depthcols) x_conversion <- x_std$x_conversion @@ -495,37 +471,31 @@ hz_intersect <- function(x, y, idcol = "id", depthcols = c("top", "bottom")) { y <- .standardize_inputs(y, idcol = idcol, depthcols = depthcols)$x # intersect x & y ---- - split(x, x$idcol) ->.; - lapply(., function(x) { + res <- lapply(split(x, x$idcol), function(x) { xi <- x yi <- y[which(y$idcol == xi$idcol[1]), ] if (nrow(yi) > 0) { - int <- c(xi$top, xi$bot, yi$top, yi$bot) |> - sort() |> - unique() + int <- unique(sort(c(xi$top, xi$bot, yi$top, yi$bot))) xi_seg <- hz_segment(xi, intervals = int, depthcols = names(x_conversion[2:3]), trim = TRUE) yi_seg <- hz_segment(yi, intervals = int, depthcols = names(x_conversion[2:3]), trim = TRUE) return(list(x_seg = xi_seg, y_seg = yi_seg)) } - }) ->.; - + }) - x_seg <- lapply(., function(x) x[["x_seg"]]) |> do.call("rbind", args = _) - y_seg <- lapply(., function(x) x[["y_seg"]]) |> do.call("rbind", args = _) - + x_seg <- do.call("rbind", lapply(res, function(x) x[["x_seg"]])) + y_seg <- do.call("rbind", lapply(res, function(x) x[["y_seg"]])) xy_int <- merge(x_seg, y_seg, by = c("segment_id", "idcol", "top", "bot"), sort = FALSE) - # reset inputs ---- xy_int <- .reset_inputs(xy_int, x_conversion) return(xy_int) - } +} @@ -552,46 +522,39 @@ hz_intersect <- function(x, y, idcol = "id", depthcols = c("top", "bottom")) { #' @examples #' #' h <- data.frame( -#' id = 1, -#' top = c(0, 25, 44, 46, 50), -#' bottom = c(25, 44, 46, 50, 100), -#' texcl = c("SL", "SL", "CL", "CL", "L"), -#' clay = c(10, 12, 27, 35, 16) +#' id = 1, +#' top = c(0, 25, 44, 46, 50), +#' bottom = c(25, 44, 46, 50, 100), +#' texcl = c("SL", "SL", "CL", "CL", "L"), +#' clay = c(10, 12, 27, 35, 16) #' ) #' -#' h |> hz_lag() +#' hz_lag(h) #' -#' h |> hz_lag(-1) +#' hz_lag(h, -1) #' -#' h |> hz_lag(10:15, unit = "depth") +#' hz_lag(h, 10:15, unit = "depth") #' -#' h |> -#' hz_lag() |> -#' cbind(h, lag = _) |> -#' transform( -#' clay_dif = lag.clay_bot.1 - clay, -#' texcl_contrast = paste0(texcl, "-", lag.texcl_bot.1)) +#' transform(cbind(h, lag = hz_lag(h)), +#' clay_dif = lag.clay_bot.1 - clay, +#' texcl_contrast = paste0(texcl, "-", lag.texcl_bot.1) +#' ) #' - - - hz_lag <- function(object, lag = 1, unit = "index", idcol = "id", depthcols = c("top", "bottom"), order = FALSE) { nm <- names(object) idx_std <- which(! nm %in% c(idcol, depthcols)) vars <- nm[idx_std] - # check arguments ---- .check_depthcols_l(depthcols) .check_names(object, vars = c(idcol, depthcols, vars)) - # standardize inputs ---- x_std <- .standardize_inputs(object, idcol = idcol, depthcols = depthcols) x_conversion <- x_std$x_conversion - x <- x_std$x; rm(x_std) - + x <- x_std$x + rm(x_std) # check depths --- if (unit == "depth" & max(object[[depthcols[2]]] > 1000)) { @@ -599,20 +562,18 @@ hz_lag <- function(object, lag = 1, unit = "index", idcol = "id", depthcols = c( x <- x[x$bot < 1000, ] } - test <- aggregate(top ~ idcol, data = x, length)$top |> max() + test <- max(aggregate(top ~ idcol, data = x, length)$top) if (unit == "index") { if ((test - 1) < max(lag)) { stop("lag can not be greater than the maximum number of horizons") } } - # order ---- if (order) { x <- x[order(x$idcol, x$top, x$bot), ] } - # lag ---- .lag_ind <- function(x, lag = lag) { @@ -630,7 +591,6 @@ hz_lag <- function(object, lag = 1, unit = "index", idcol = "id", depthcols = c( return(x_lag) } - .lag_dep <- function(x, lag = lag) { n <- length(x) @@ -638,71 +598,69 @@ hz_lag <- function(object, lag = 1, unit = "index", idcol = "id", depthcols = c( x_seg <- hz_segment(x, intervals = min(x$top):max(x$bot), trim = TRUE, depthcols = c("top", "bot")) x_seg <- x_seg[1:(n + 1)] - - x_seg <- lapply(lag, function(i) { - + x_seg <- do.call("cbind", args = lapply(lag, function(i) { x$bot_i <- x$bot + i - idx <- match( - paste(x$idcol, x$bot_i), - paste(x_seg$idcol, x_seg$bot) - ) - xi_seg <- x_seg[idx, ] + idx <- match(paste(x$idcol, x$bot_i), + paste(x_seg$idcol, x_seg$bot)) + xi_seg <- x_seg[idx,] xi_seg <- x[xi_seg$.ID, vars, drop = FALSE] xi_seg$.ID <- NULL - if (i >= 0) names(xi_seg) <- paste0(names(xi_seg), "_bot.", i) - if (i < 0) names(xi_seg) <- paste0(names(xi_seg), "_top.", abs(i)) + if (i >= 0) + names(xi_seg) <- paste0(names(xi_seg), "_bot.", i) + if (i < 0) + names(xi_seg) <- paste0(names(xi_seg), "_top.", abs(i)) return(xi_seg) - }) |> - do.call("cbind", args = _) + })) return(x_seg) } - if (unit == "index") { - x_lag <- lapply(lag, function(i) { + x_lag <- do.call("cbind", lapply(lag, function(i) { .lag_ind(x, i) - }) |> - do.call("cbind", args = _) + })) x_lag <- x_lag[sort(names(x_lag))] } + if (unit == "depth") { x_lag <- .lag_dep(x, lag) x_lag <- x_lag[sort(names(x_lag))] } - # # reset inputs ---- x_lag <- .reset_inputs(x_lag, x_conversion) - return(x_lag) } - - # check depthcols length .check_depthcols_l <- function(x) { if (length(x) != 2 & !is.null(x)) stop("depthcols must length must equal 2") } - ## check for matching column names .check_names <- function(x, vars) { - x_nm <- names(x) - if (! all(vars %in% x_nm)) { + if (!all(vars %in% x_nm)) { stop("x must contain columns with names that match the input arguments") } } - # standardize inputs -.standardize_inputs <- function(x, idcol = NULL, hzidcol = NULL, depthcols = NULL, texcl = NULL, clay = NULL, taxpartsize = NULL, sand = NULL) { +.standardize_inputs <- function( + x, + idcol = NULL, + hzidcol = NULL, + depthcols = NULL, + texcl = NULL, + clay = NULL, + taxpartsize = NULL, + sand = NULL +) { # set new names var_names <- c( @@ -731,7 +689,9 @@ hz_lag <- function(object, lag = 1, unit = "index", idcol = "id", depthcols = c( idx_orig <- idx_dup[! idx_dup %in% idx_x] names(x)[idx_orig] <- paste0(names(x)[idx_orig], "_orig") - } else idx_orig = NULL + } else { + idx_orig <- NULL + } return(list(x = x, x_conversion = var_names, x_orig = idx_orig)) } @@ -747,4 +707,4 @@ hz_lag <- function(object, lag = 1, unit = "index", idcol = "id", depthcols = c( return(x) } - \ No newline at end of file + diff --git a/R/texture.R b/R/texture.R index 70330a8f..bc803348 100644 --- a/R/texture.R +++ b/R/texture.R @@ -1081,7 +1081,6 @@ fragvol_to_texmod <- function( #' lookup_taxpartsize <- function() { - fe <- c("diatomaceous", "very-fine", "clayey", "fine", "hydrous", "fine-silty", "fine-gypseous", "fine-loamy", "medial", "loamy", "coarse-loamy", "coarse-silty", "coarse-gypseous", "ashy", "sandy", "hydrous-pumiceous", @@ -1100,7 +1099,7 @@ lookup_taxpartsize <- function() { names(test) <- .pscs_sc idx <- lapply(test, function(x) { - idx <- sapply(x, function(y) rank[which(fe == y)]) |> unlist() + idx <- unlist(sapply(x, function(y) rank[which(fe == y)])) # select the 3rd value when "or" results in 3 values if (length(idx) > 2) idx <- c(idx[1], idx[3]) @@ -1132,5 +1131,4 @@ lookup_taxpartsize <- function() { } -.pscs_sc <- c("Ashy over clayey", "Ashy over clayey-skeletal", "Ashy over loamy", "Ashy over loamy-skeletal", "Ashy over medial", "Ashy over medial-skeletal", "Ashy over pumiceous or cindery", "Ashy over sandy or sandy-skeletal", "Ashy-skeletal over clayey", "Ashy-skeletal over fragmental or cindery", "Ashy-skeletal over loamy-skeletal", "Ashy-skeletal over sandy or sandy-skeletal", "Cindery over loamy", "Cindery over medial", "Cindery over medial-skeletal", "Clayey over coarse-gypseous", "Clayey over fine-gypseous", "Clayey over fragmental", "Clayey over gypseous-skeletal", "Clayey over loamy", "Clayey over loamy-skeletal", "Clayey over sandy or sandy-skeletal", "Clayey-skeletal over sandy or sandy-skeletal", "Coarse-loamy over clayey", "Coarse-loamy over fragmental", "Coarse-loamy over sandy or sandy-skeletal", "Coarse-silty over clayey", "Coarse-silty over sandy or sandy-skeletal", "Fine-loamy over clayey", "Fine-loamy over fragmental", "Fine-loamy over sandy or sandy-skeletal", "Fine-silty over clayey", "Fine-silty over fragmental", "Fine-silty over sandy or sandy-skeletal", "Hydrous over clayey", "Hydrous over clayey-skeletal", "Hydrous over fragmental", "Hydrous over loamy", "Hydrous over loamy-skeletal", "Hydrous over sandy or sandy-skeletal", "Loamy over ashy or ashy-pumiceous", "Loamy over coarse-gypseous", "Loamy over fine-gypseous", "Loamy over pumiceous or cindery", "Loamy over sandy or sandy-skeletal", "Loamy-skeletal over cindery", "Loamy-skeletal over clayey", "Loamy-skeletal over fragmental", "Loamy-skeletal over gypseous-skeletal", "Loamy-skeletal over sandy or sandy-skeletal", "Medial over ashy", "Medial over ashy-pumiceous or ashy-skeletal", "Medial over clayey", "Medial over clayey-skeletal", "Medial over fragmental", "Medial over hydrous", "Medial over loamy", "Medial over loamy-skeletal", "Medial over pumiceous or cindery", "Medial over sandy or sandy-skeletal", "Medial-skeletal over fragmental or cindery", "Medial-skeletal over loamy-skeletal", "Medial-skeletal over sandy or sandy-skeletal", "Pumiceous or ashy-pumiceous over loamy", "Pumiceous or ashy-pumiceous over loamy-skeletal", "Pumiceous or ashy-pumiceous over medial", "Pumiceous or ashy-pumiceous over medial-skeletal", "Pumiceous or ashy-pumiceous over sandy or sandy-skeletal", "Sandy over clayey", "Sandy over loamy", "Sandy-skeletal over loamy") |> - tolower() +.pscs_sc <- tolower(c("Ashy over clayey", "Ashy over clayey-skeletal", "Ashy over loamy", "Ashy over loamy-skeletal", "Ashy over medial", "Ashy over medial-skeletal", "Ashy over pumiceous or cindery", "Ashy over sandy or sandy-skeletal", "Ashy-skeletal over clayey", "Ashy-skeletal over fragmental or cindery", "Ashy-skeletal over loamy-skeletal", "Ashy-skeletal over sandy or sandy-skeletal", "Cindery over loamy", "Cindery over medial", "Cindery over medial-skeletal", "Clayey over coarse-gypseous", "Clayey over fine-gypseous", "Clayey over fragmental", "Clayey over gypseous-skeletal", "Clayey over loamy", "Clayey over loamy-skeletal", "Clayey over sandy or sandy-skeletal", "Clayey-skeletal over sandy or sandy-skeletal", "Coarse-loamy over clayey", "Coarse-loamy over fragmental", "Coarse-loamy over sandy or sandy-skeletal", "Coarse-silty over clayey", "Coarse-silty over sandy or sandy-skeletal", "Fine-loamy over clayey", "Fine-loamy over fragmental", "Fine-loamy over sandy or sandy-skeletal", "Fine-silty over clayey", "Fine-silty over fragmental", "Fine-silty over sandy or sandy-skeletal", "Hydrous over clayey", "Hydrous over clayey-skeletal", "Hydrous over fragmental", "Hydrous over loamy", "Hydrous over loamy-skeletal", "Hydrous over sandy or sandy-skeletal", "Loamy over ashy or ashy-pumiceous", "Loamy over coarse-gypseous", "Loamy over fine-gypseous", "Loamy over pumiceous or cindery", "Loamy over sandy or sandy-skeletal", "Loamy-skeletal over cindery", "Loamy-skeletal over clayey", "Loamy-skeletal over fragmental", "Loamy-skeletal over gypseous-skeletal", "Loamy-skeletal over sandy or sandy-skeletal", "Medial over ashy", "Medial over ashy-pumiceous or ashy-skeletal", "Medial over clayey", "Medial over clayey-skeletal", "Medial over fragmental", "Medial over hydrous", "Medial over loamy", "Medial over loamy-skeletal", "Medial over pumiceous or cindery", "Medial over sandy or sandy-skeletal", "Medial-skeletal over fragmental or cindery", "Medial-skeletal over loamy-skeletal", "Medial-skeletal over sandy or sandy-skeletal", "Pumiceous or ashy-pumiceous over loamy", "Pumiceous or ashy-pumiceous over loamy-skeletal", "Pumiceous or ashy-pumiceous over medial", "Pumiceous or ashy-pumiceous over medial-skeletal", "Pumiceous or ashy-pumiceous over sandy or sandy-skeletal", "Sandy over clayey", "Sandy over loamy", "Sandy-skeletal over loamy")) diff --git a/man/hz_intersect.Rd b/man/hz_intersect.Rd index 304e85a7..6841ec1b 100644 --- a/man/hz_intersect.Rd +++ b/man/hz_intersect.Rd @@ -27,21 +27,19 @@ This function intersects two horizon tables by harmonizing their depths and merg \examples{ h <- data.frame( -id = 1, -top = c(0, 25, 44, 46, 50), -bottom = c(25, 44, 46, 50, 100), -by = c("Yes", "Yes", "No", "No", "Yes"), -clay = c(10, 12, 27, 35, 16) + id = 1, + top = c(0, 25, 44, 46, 50), + bottom = c(25, 44, 46, 50, 100), + by = c("Yes", "Yes", "No", "No", "Yes"), + clay = c(10, 12, 27, 35, 16) ) -h |> hz_dissolve("by") +hz_dissolve(h, "by") -h |> hz_dissolve("by") |> hz_intersect(x = _, y = h) +hz_intersect(x = hz_dissolve(h, "by"), y = h) -h |> -hz_dissolve("by") |> -hz_intersect(x = h, y = _) |> -aggregate(clay ~ dissolve_id, data = _, mean) +hi <- hz_intersect(x = h, y = hz_dissolve(h, "by")) +aggregate(clay ~ dissolve_id, data = hi, mean) } \seealso{ diff --git a/man/hz_lag.Rd b/man/hz_lag.Rd index 1691dc65..4acb253b 100644 --- a/man/hz_lag.Rd +++ b/man/hz_lag.Rd @@ -38,25 +38,23 @@ This function finds adjacent values to a horizon values at lagged distances. \examples{ h <- data.frame( -id = 1, -top = c(0, 25, 44, 46, 50), -bottom = c(25, 44, 46, 50, 100), -texcl = c("SL", "SL", "CL", "CL", "L"), -clay = c(10, 12, 27, 35, 16) + id = 1, + top = c(0, 25, 44, 46, 50), + bottom = c(25, 44, 46, 50, 100), + texcl = c("SL", "SL", "CL", "CL", "L"), + clay = c(10, 12, 27, 35, 16) ) -h |> hz_lag() +hz_lag(h) -h |> hz_lag(-1) +hz_lag(h, -1) -h |> hz_lag(10:15, unit = "depth") +hz_lag(h, 10:15, unit = "depth") -h |> -hz_lag() |> -cbind(h, lag = _) |> -transform( -clay_dif = lag.clay_bot.1 - clay, -texcl_contrast = paste0(texcl, "-", lag.texcl_bot.1)) +transform(cbind(h, lag = hz_lag(h)), + clay_dif = lag.clay_bot.1 - clay, + texcl_contrast = paste0(texcl, "-", lag.texcl_bot.1) +) } \seealso{ diff --git a/man/hz_to_taxpartsize.Rd b/man/hz_to_taxpartsize.Rd index 2daefdf8..8a477fd2 100644 --- a/man/hz_to_taxpartsize.Rd +++ b/man/hz_to_taxpartsize.Rd @@ -38,34 +38,29 @@ This function differs from \code{\link{texture_to_taxpartsize}} in that is aggre \examples{ h <- data.frame( -id = 1, -hzname = c("A", "BA", "Bw", "BC", "C"), -top = c( 0, 10, 45, 60, 90), -bottom = c(10, 45, 60, 90, 150), -clay = c(15, 16, 45, 20, 10), -sand = c(10, 35, 40, 50, 90), -frags = c( 0, 5, 10, 38, 40) + id = 1, + hzname = c("A", "BA", "Bw", "BC", "C"), + top = c(0, 10, 45, 60, 90), + bottom = c(10, 45, 60, 90, 150), + clay = c(15, 16, 45, 20, 10), + sand = c(10, 35, 40, 50, 90), + frags = c(0, 5, 10, 38, 40) ) -h <- cbind( -h, -texcl = ssc_to_texcl(clay = h$clay, sand = h$sand) -) +h <- cbind(h, + texcl = ssc_to_texcl(clay = h$clay, sand = h$sand)) -pscs <- data.frame( -id = 1, -top = 25, -bottom = 100 -) +pscs <- data.frame(id = 1, + top = 25, + bottom = 100) -h <- cbind( -h, -taxpartsize = texture_to_taxpartsize( -texcl = h$texcl, -clay = h$clay, -sand = h$sand, -fragvoltot = h$frags -)) +h <- cbind(h, + taxpartsize = texture_to_taxpartsize( + texcl = h$texcl, + clay = h$clay, + sand = h$sand, + fragvoltot = h$frags + )) depths(h) <- id ~ top + bottom