我是R的新手,想将其用于我的数据分析和可视化。
我有一个大约38575行(像素)和600列的数据框架。每列包含分析物的强度,导致每个像素的光谱。我也有每个像素的x和y坐标来创建一个数据立方体(数组),从某种意义上说,如果我说image_cube [1,1,]给我第一个频谱,如果我说image_cube [,, 1],i获取所有像素的图像,显示第一个分析物的强度。并非所有像素都有一个频谱,并且不在数据框架中,它们应该只是空像素(黑色)。
编辑
我尝试使用以下代码,而ROI数据是大数据框架和sample_overview,每个像素的变量包含x和y坐标:
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,]
}
但是我会收到以下错误:
Error in ROI_cube[sample_overview[i, 1], sample_overview[i, 2], ] <- ROI_data[i, :
incorrect number of subscripts
如果我正确地收到您的问题,您想通过已知的X-Y坐标将光谱的2D数据框架映射到3D阵列。如果这就是您想要的,那么您不需要任何软件包,只是将数据框中的数据映射到数组
的问题 #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