Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions R/aggregate_spectra.R
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,30 @@ setMethod("aggregate_spectra", "SpectraDataFrame",

res <- SpectraDataFrame(res, data = data.frame(matrix(data, nrow = 1, dimnames = list(id, names(obj)))))
}

# There is a variable against which the data will be aggregated
else {
if (id %in% names(features(obj))) {

if (id %in% names(features(obj))) {
# Col index of the splitting variable
idx <- which(names(features(obj)) == id)

idx <- paste("(names(features(obj)) == '", id, "')", sep = "")
idx <- paste(idx, collapse="|")
idx <- which(eval(parse(text=idx)))

# Creating spectra splits
s <- data.frame(id = features(obj)[, idx, drop = FALSE], spectra(obj))

s <- ddply(s, id, colwise(fun), ...)
colnames(s) <- gsub("id.", "", colnames(s))
s <- na.omit(ddply(s, id, colwise(fun), ...))

# Remove id used to split data.frame
s <- s[, -1]

s <- s[, -(1:(length(id)))]
# new data slot
d <- ddply(features(obj), id, colwise(fun, ...))
feat <- merge(subset(features(obj), select=c(id)), Filter(is.numeric, features(obj)), by="row.names", suffixes = "")
rownames(feat) <- feat[,1]
feat <- feat[,-1]
d <- ddply(feat, id, colwise(fun), .drop=TRUE, ...)
d <- na.omit(d[colSums(!is.na(d)) > 0])

# recompose the object
res <- SpectraDataFrame(wl = wl(obj), nir = s, units = wl_units(obj), data = d)
Expand All @@ -122,4 +129,4 @@ setMethod("aggregate_spectra", "SpectraDataFrame",

res
}
)
)