0

I am new to R and would like to use it for my data analysis and visualization.

I have a dataframe with about 38575 rows (pixels) and 600 columns. Each column contains the intensity of an analyte, resulting in a spectrum per pixel. I also have x and y coordinates for each pixel to create a data cube(array), in the sense that if I say image_cube[1,1,] gives me the first spectrum and if I say image_cube[,,1], I get an image of all pixels showing the intensity for the first analyte. Not all pixels have a spectrum and they are not in the dataframe, these should just be empty pixels (black).

EDIT

I tried to use the following code with ROI data being the big dataframe and sample_overview the variable containing x and y coordinates for each pixel:

ROI_cube <- array(rep(0, 311*381*603), dim=c(311, 381, 603))   
for (i in 1:dim(ROI_data)[1]) {
  ROI_cube[sample_overview[i,2], sample_overview[i,1],] = ROI_data[i,]
}

But I get the following error:

Error in ROI_cube[sample_overview[i, 1], sample_overview[i, 2], ] <- ROI_data[i,  : 
  incorrect number of subscripts
  • Welcome to SO! Please provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a desired output (or a mock up of one). Asking for recommendations on packages/libraries is considered off-topic so please re-word your question and gear it towards what you are trying to accomplish – emilliman5 Dec 19 '18 at 15:42

1 Answers1

0

If i get your question correctly you want to map a 2D Dataframe of Spectra through known x-y Coordinates to a 3D-Array. If thats all you want, you don't need any packages, it's just a matter of mapping the data in the dataframe to an Array

  #Simulate some gaussian spectra
  set.seed(1234)
  simSpec <- function()
  {
    x <- 1:400
    y <- stats::dnorm(x,mean=runif(1,min=0,max=400),sd=runif(1,min=2,max=50))
    return(y)
  }

  #build a dataframe
  data <- data.frame(matrix(data=NA,nrow=400,ncol=20))
  for(i in 1:20) data[,i] <- simSpec()

  #assume data is ordered in ascending x/y pixels 
  #=> data[,1] -> x=1, y=1 ; data[,2] -> x=2, y=1; data[,length(x)] -> x=length(x), y=y; 
  #data[,m+(n-1)*length(x)] -> x=m, y=n

  Array <- array(data=t(data),dim=c(5,4,400)) #Build Array of format [X,Y,NSpectralVariables]
                                              #transpose dataframe because default order is to first increase Columnnumber

  plot(Array[1,1,],type="l") # Plot Spectrum at x=1, y=1
  contour(Array[,,1]) #Contour Intensity at first Analyte
Julian_Hn
  • 2,086
  • 1
  • 8
  • 18
  • Thank you, this does create the cube I wanted! But I have the feeling that the spectra are not at the correct coordinates. Is there also a way to fill the array via the coordinate variable? In my coordinate variable I see that the first pixel ID corresponds with x-coord 350 and y-coord 300, so the first row of data should be at the pixel with [350,300] coordinates. And this spectrum only contains zeros because it was deleted after QC. Now when I do plot(Array[350,300,], type="l"), it does show a spectra while should be flat line. – Leah Leeuwen Jan 07 '19 at 10:51
  • Are you sure you have the spectra ordered in the way I commented in my answer? If this is the case normally it should be exactly like you desire – Julian_Hn Jan 08 '19 at 13:23