meireles / spectrolab

Dealing with spectral data in R
16 stars 11 forks source link

Warnings associated with aggregate.spectra #12

Closed LanceStasinski closed 4 years ago

LanceStasinski commented 4 years ago

aggregate-spectra-warnings.pdf

meireles commented 4 years ago

@LanceStasinski Thx for reporting. Can you please add the script directly into the comment box (instead of uploading a pdf). Thanks!

LanceStasinski commented 4 years ago

Issue: Aggregate.spectra produces warnings when the try_keep_text() argument is not specified.

Example: Let's say I want to apply a function to achillea_spec subsetted by the plot they were collected in (reffered to as plotNumber).

#load data
library(spectrolab)
dir_path = system.file("extdata/spec_matrix_meta.csv", package = "spectrolab")
spec_csv = read.csv(dir_path, check.names = FALSE)
achillea_spec = as.spectra(spec_csv, name_idx = 3, meta_idxs = c(1,2) )

#add plotNumber column to metadata
data = meta(achillea_spec)
data$plotNumber <- sample(1:3, replace = T, nrow(data))
meta(achillea_spec) <- data

#use aggregate function
ag.achillea_spec1 = aggregate(achillea_spec, by = data$plotNumber, mean)

which outputs:

Warning messages:
1: In mean.default(X[[i]], ...) :
  argument is not numeric or logical: returning NA

ag.achillea_spec1
spectra object 
number of samples: 3 
bands: 400 to 2400 (2001 bands)
metadata (3): ident, ssp, plotNumber

This results in the error above; however, it appears that the spectra object was still recognized.

Now let's add the try_keep_text() argument.

ag.achillea_spec2 = aggregate(achillea_spec, by = data$plotNumber, mean, 
                              try_keep_txt(mean))
ag.achillea_spec2
spectra object 
number of samples: 3 
bands: 400 to 2400 (2001 bands)
metadata (3): ident, ssp, plotNumber

aggregate() now recognizes the spectra object with no associated warning.

I hope this is useful for you.

meireles commented 4 years ago

@LanceStasinski The warning happens because aggregate is trying to take the mean of the metadata. The metadata only has character objects in this case, and it doesn't make sense to take the mean of characters.

So the behavior is expected. I agree that the warning message is cryptic though. I'll update it so the issue becomes more obvious.

LanceStasinski commented 4 years ago

That makes sense. Thank you!

meireles commented 4 years ago

I'll leave this open until I update the error message. Thx!

meireles commented 4 years ago

fixed in 583f7e3046e362b115a95a8f9235f3ca9ca60106