I am dealing with a spatial dataset that I need to divide into a training and a validation subset.
To be specific, I have a raster with 31 bands; I need to use all of them as parameters for the division of the dataset into the two subsets. I wish to use the Kennard-Stone algorithm for the division, so I have looked into the two existing functions that can be used in R
.
The first is the ken.sto
in the soil.spec
package. The second is the duplex
in the cran
package. The problem is that both of them require a matrix or a dataframe as the input, while I have a multilayer raster that I can only convert into an array.
Does anyone have any suggestion on how to transform my spatial data, so that it can be used in one of the KS functions?
If
ras
is you stack, you can useas.data.frame(ras)
to convert your multilayer raster into adata.frame
. This will result in a two-dimensionaldata.frame
with n columns (n = raster layers, in your case 31) and m rows (m = number of cells in your raster). Then you should be able to apply thesoil.spec
function which requires adata.frame
as input.Note: You will however lose the spatial information if you convert your
raster
to adata.frame
. After you have applied your sampling you might want to export the result back as a raster. Here you can use the indices of thedata.frame
rows to get the values back into the initialraster
grid.