Convert wide to long with the function pivot_longer (library tidyr)
Sometimes we need to convert our hyperspectral object into data.frame. And for some visualization methods we need to have this data.frame in a long format (one column with wavenumbers another with the intesities and third with spectrum description: in the code chunk below with cluster number: Cl1 or Cl2 or Cl3) not in a wide format (first column with wavenumbers and subsequent columns with the spectra and their intensities). Function hyperSpec::as.t.df
gives wide version as the output. We could use tidyr::pivot_longer
function to convert it to long format. First argument tells which columns should be converted (here all except Wavenumber), names_to
defines the name of the variable describing the spectrum,values_to
define what should be the values for this variable.
library(tidyr)
mean_cl3 <- as.t.df(mean_clusters3)
colnames(mean_cl3)<-c("Wavenumber", "Cl1","Cl2","Cl3")
mean_cl3_long<-mean_cl3 %>%pivot_longer(!Wavenumber, names_to = "Cluster", values_to = "mean")