1

I am analyzing flower color spectra with the pavo package in R, and need to average all spectra from the same species. Spectra are all in one folder so I need to read all of them separately and average them so that I end up with one mean spectrum per plant species. I have different number of spectra for each species.

How do I specify to pavo that I want to aggregate spectra based on, say, the first four letters of each spectrum? In the example below, I would like to aggregate all polygal spectra and all larcun spectra. The pavo manual isn´t helping me and I don´t see any posts on pavo in this forum. Thanks in advance for any help.

I tried with the function aggspec, by I have not managed to aggregate based on the first letters of each spectrum file name.

I have only managed so far to read all the spectra, plot them, and obtain the variables that I need (hue, birghtness, chroma) for each file separately, but I need to average the spectra by species instead.

library(pavo)
#I create fake spectra, for two individuals of polygal species and three of larcun species
wl = seq(300, 560, 10)
polyg1 = c(2.381,  2.758,  2.923,  2.883,  3.127,  3.365,  3.364,  3.341,  3.42,  3.507,  3.534,  3.654, 3.782, 3.96, 4.125, 4.4,  4.691,  5.275,  5.888, 6.563,  7.275,  8.138,  8.866,  9.591, 10.121,  10.565,  10.895) 
polyg22 = spec1 + runif(27, 0, 2)
larcun1 = spec1 + runif(27, 0, 2)
larcun2 = spec1 + runif(27, 0, 2)
larcun3 = spec1 + runif(27, 0, 2)

all.specs = cbind (wl, polyg1, polyg22, larcun1, larcun2, larcun3)
spectra1 = as.rspec(all.specs, interp = FALSE)
spectra.smooth = procspec(spectra1, opt = "smooth")
plot(spectra.smooth)
explorespec(spectra.smooth)
  • Per the R tag (hover to see): Please provide minimal and [reproducible example(s)](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) along with the desired output. Use `dput()` for data. So, can you provide a sample: `dput(head(spectra.smooth[,1:5], 10))`? – Parfait Apr 23 '19 at 14:02
  • I now added some specs to illustrate my problem. The code isn´t very elegant but I managed to create a simple example of what my data look like. Thanks! – Silvia Lomascolo Apr 23 '19 at 18:09

1 Answers1

1

Consider using the special by variant of pavo::aggspec:

by
... a vector containing identifications for the columns in the spectra data frame (in which case the function will be applied to each group of spectra sharing the same identification);

Specifically, create a character vector that tags your needed species grouped by first five letters of column headers. Note: the wl if it exists is ignored in this call.

species <- substr(names(spectra.smooth), 1, 5)

species_agg <- aggspec(spectra.smooth, by=species, FUN=mean)

Output (with column headers as values in character vector)

species_agg
#     wl  species1  species2  species3  ...
# 1  100   ##.####   ##.####   ##.####   
# 2  101   ##.####   ##.####   ##.####   
# 3  102   ##.####   ##.####   ##.####   
# ...
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • YES!! It worked perfectly. Thank you so much Parfait! – Silvia Lomascolo Apr 23 '19 at 18:14
  • Done. Proper ackowledgement has been given now, accepting your answer. I tried "voting" for your answer too, but it says that it won´t show as I do not use much Stackoverflow so my reputation is to low to show my vote. – Silvia Lomascolo Apr 24 '19 at 13:04