如何将数据框转换为 raster::stack 对象?

How To convert dataframe to raster::stack object?

我想知道是否有办法将数据帧转换为 aster::stack 对象? 我的数据:

xy<- structure(list(X = c(-179.75, -179.75, -179.75, -179.75, -179.75, 
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75, 
-179.75, -179.75, -179.75, -179.75, -179.75, -179.75, -179.75, 
-179.75), Y = c(-52.25, -51.75, -51.25, -50.75, -50.25, -49.75, 
-49.25, -48.75, -48.25, -47.75, -47.25, -46.75, -46.25, -45.75, 
-45.25, -44.75, -44.25, -43.75, -43.25, -42.75)), row.names = c(NA, 
20L), class = "data.frame")
    df<- structure(list(SST = c(7.59180453767379, 7.73509759058555, 7.84841126371175, 
7.93174555705239, 8.07323373736193, 8.27287580464035, 8.60783228824536, 
9.07810318817695, 9.55298856149117, 10.032488408188, 10.7857288212205, 
11.8127098005886, 12.6318128572156, 13.2430379911015, 13.5948196913426, 
13.6871579579388, 13.9124653160572, 14.2707417656978, 14.9238792363554, 
15.8718777280301), SSS = c(34.0450506702065, 34.0508728072047, 
34.0571919595202, 34.0640081271529, 34.0731531754136, 34.0846271043022, 
34.0977436259389, 34.1125027403235, 34.132830452919, 34.1587267637253, 
34.2435973445574, 34.3874421954155, 34.5183364008864, 34.6362799609701, 
34.6962110688289, 34.6981297244628, 34.7175911421577, 34.7545953219136, 
34.8712614620725, 35.0675895626346), SO2 = c(0.299824322961892, 
0.299422515447562, 0.299219067511149, 0.299213979152652, 0.298692794353701, 
0.297655513114296, 0.2957078315046, 0.292849749524612, 0.289952534728218, 
0.287016187115417, 0.282462434495877, 0.276291276869597, 0.271238694060594, 
0.267304686068868, 0.265136273610794, 0.264733456686372, 0.263699139289868, 
0.262033321421283, 0.258719810688247, 0.253758607090761), NPP = c(3.85156376257745e-07, 
3.96088458405608e-07, 4.11107894580006e-07, 4.30214684780938e-07, 
4.4703424908014e-07, 4.61566587477609e-07, 4.77750944139858e-07, 
4.95587319066885e-07, 5.13265392578418e-07, 5.30785164674456e-07, 
5.50156834773034e-07, 5.71380402874152e-07, 5.84950195207471e-07, 
5.90866211772993e-07, 5.98413147197737e-07, 6.07591001481704e-07, 
6.22490969298673e-07, 6.43113050648643e-07, 6.34808436525414e-07, 
5.97577126928983e-07), MLD = c(108.645480087648, 104.108556013554, 
98.3433291949332, 92.578102376312, 86.8128755576909, 81.6714904400887, 
77.1539470235055, 72.6364036069223, 68.1188601903391, 65.1494404875645, 
63.7281444985984, 62.3068485096323, 60.8855525206661, 59.9668972487703, 
59.5508826939446, 59.1348681391189, 58.7188535842932, 59.1232465262892, 
60.3480469651069, 61.5728474039246), SIC = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 
20L), class = "data.frame")

您可以使用

r = stack(lapply(df, function(j) raster(matrix(j, nrow=5))))

# class      : RasterStack 
# dimensions : 5, 4, 20, 6  (nrow, ncol, ncell, nlayers)
# resolution : 0.25, 0.2  (x, y)
# extent     : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
# crs        : NA 
# names      :          SST,          SSS,          SO2,          NPP,          MLD,          SIC 
# min values : 7.591805e+00, 3.404505e+01, 2.537586e-01, 3.851564e-07, 5.871885e+01, 0.000000e+00 
# max values : 1.587188e+01, 3.506759e+01, 2.998243e-01, 6.431131e-07, 1.086455e+02, 0.000000e+00 

您没有指定栅格图层的尺寸,所以在上面,我假设有 5 行。您需要指定此维度以适合您的数据,因为它不能直接从数据中推断出来。

stack_raster <- function(ENV, resp.xy ) {
  r <- stack()
  for (col in 1:ncol(ENV)) {
    ENV_col <- rasterFromXYZ(cbind(resp.xy, ENV[, col]), digits = 5 )
    r <- stack(r , ENV_col)
    
  }
  names(r) = colnames(ENV)
  return(r)
}

r <- stack_raster(ENV, resp.xy)