Skip to content

Commit

Permalink
Use data.table for all j-index subsetting #157
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Feb 15, 2021
1 parent dfa98a4 commit b92c41c
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions R/SoilProfileCollection-operators.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,17 @@ setMethod("[", signature(x = "SoilProfileCollection",
if (!missing(j)) {

# faster replacement of j subsetting of horizon data
if (aqp_df_class(x) == "data.table") {
# if (aqp_df_class(x) == "data.table") {

h <- as.data.table(h)

# local vars to make R CMD check happy
.N <- NULL
.I <- NULL
V1 <- NULL

# data.table can do this much more efficiently
if (requireNamespace("data.table", quietly = TRUE)) {
# if (requireNamespace("data.table", quietly = TRUE)) {
idn <- idname(x)

# by list @horizons idname (essentially iterating over profiles)
Expand All @@ -154,25 +156,24 @@ setMethod("[", signature(x = "SoilProfileCollection",
pids <- h[, .I[any(1:.N %in% j)][1], by = bylist]
i.idx <- pids[, .I[!is.na(V1)]]
}
}

} else {
# retain a base R way of doing things (plenty fast with SPCs up to ~100k or so)
j.res <- as.list(aggregate(
h[[hzidname(x)]],
by = list(h[[idname(x)]]),
FUN = function(hh) {
list(1:length(hh) %in% j)
},
drop = FALSE
)$x)

## https://github.com/ncss-tech/aqp/issues/89
# fix #89, where i with no matching j e.g. @site data returned
i.idx <- which(as.logical(lapply(j.res, function(jr) { any(jr) })))

j.idx <- which(do.call('c', j.res))
}
# }
# } else {
# # retain a base R way of doing things (plenty fast with SPCs up to ~100k or so)
# j.res <- as.list(aggregate(
# h[[hzidname(x)]],
# by = list(h[[idname(x)]]),
# FUN = function(hh) {
# list(1:length(hh) %in% j)
# },
# drop = FALSE
# )$x)
#
# ## https://github.com/ncss-tech/aqp/issues/89
# # fix #89, where i with no matching j e.g. @site data returned
# i.idx <- which(as.logical(lapply(j.res, function(jr) { any(jr) })))
#
# j.idx <- which(do.call('c', j.res))
# }

# find any index out of bounds and ignore them
# j.idx.bad <- which(abs(j.idx) > nrow(h))
Expand Down

0 comments on commit b92c41c

Please sign in to comment.