假设我有一组在3D空间中不同位置进行的测量。测量值的位置具有坐标矢量
x <- c(0,1,2,3)
y <- c(4,5,6)
z <- c(7,8)因此,例如,最接近原点的测量是在location=(0,4,7)完成的。根据上面的坐标向量,我想创建一个3D数组--而且只是一个数组。@bnaul:这些是我想要赋值的体素中心的坐标。我的意图是,在pcode中,
arr <- magic( c(0,1,2,3) , c(4,5,6) , c(7,8) )
# arr is now a 3D array filled with NAs
value1 -> arr[0, 4, 7]
value2 -> arr[3, 5, 7]
# and so on, but if one does
valueBad -> arr[4,3,2] # one should get an error, as should, e.g.,
valueBad2 -> arr[3,4,5]但是我怀疑我已经“用NetCDF思考”太久了:基本上,我想要做的是将coordinates赋值给一个数组,我不相信在R中可以做到这一点。
发布于 2013-02-07 07:17:34
# starting data
x <- c(0,1,2,3)
y <- c(4,5,6)
z <- c(7,8)
# find every combo
w <- expand.grid( x , y , z )
# convert to a matrix
v <- as.matrix( w )
# view your result
v发布于 2013-02-07 07:32:06
或者,这也可能有所帮助。请澄清您想要的结果:)
# starting data
x <- c(0,1,2,3)
y <- c(4,5,6)
z <- c(7,8)
# create a 4 x 3 x 2 array
v <- 
    array( 
        # start out everything as missing..
        NA , 
        # ..and make the lengths of the dimensions the three lengths.
        dim = 
            c( length( x ) , length( y ) , length( z ) ) 
    )
# view your result
v
# now populate it with something..
# for now, just populate it with 1:24
v[ , , ] <- 1:length(v)
# view your result again
v发布于 2013-02-07 08:28:27
 array(NA, dim=c(4,3,2), 
   dimnames=list( x = c(0,1,2,3),
     y = c(4,5,6),
     z = c(7,8) ) )
, , z = 7
   y
x    4  5  6
  0 NA NA NA
  1 NA NA NA
  2 NA NA NA
  3 NA NA NA
, , z = 8
   y
x    4  5  6
  0 NA NA NA
  1 NA NA NA
  2 NA NA NA
  3 NA NA NAhttps://stackoverflow.com/questions/14740563
复制相似问题