Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何从n维数组中选择子数组?_R_Multidimensional Array - Fatal编程技术网

R 如何从n维数组中选择子数组?

R 如何从n维数组中选择子数组?,r,multidimensional-array,R,Multidimensional Array,短版: 当n为任意值时,如何以编程方式从n维数组中选择子数组 (如果这个问题的简短版本足够清楚,请跳过本文的其余部分。) 假设A是一个数组,使得dim(A)是n>2的正整数(d1,d2,…,dn)的向量 例如: > d <- 5:2 > set.seed(0) > A <- array(runif(prod(d)), dim = d) 更一般地说,如果 1&leq;k1也许这对你有用: func <- function(ary, ..., drop =

短版:

当n为任意值时,如何以编程方式从n维数组中选择子数组

(如果这个问题的简短版本足够清楚,请跳过本文的其余部分。)


假设
A
是一个数组,使得
dim(A)
是n>2的正整数(d1,d2,…,dn)的向量

例如:

> d <- 5:2
> set.seed(0)
> A <- array(runif(prod(d)), dim = d)

更一般地说,如果
1&leq;k1也许这对你有用:

func <- function(ary, ..., drop = TRUE) {
  d <- length(dim(ary))
  dots <- list(...)
  if (length(dots) > d) stop("incorrect number of dimensions")
  rest <- rep(TRUE, d - length(dots))
  do.call(`[`, c(list(ary), c(dots, rest, drop = drop)))
}
此函数为您“填充”其余部分:

func(A, 3, 2)
#            [,1]      [,2]
# [1,] 0.94467527 0.4785452
# [2,] 0.01339033 0.7111212
# [3,] 0.02333120 0.1293723

func(A, 3)
# , , 1
#           [,1]       [,2]      [,3]
# [1,] 0.3721239 0.21214252 0.6470602
# [2,] 0.9446753 0.01339033 0.0233312
# [3,] 0.1765568 0.59956583 0.8612095
# [4,] 0.7176185 0.79423986 0.3162717
# , , 2
#           [,1]       [,2]      [,3]
# [1,] 0.2936034 0.71251468 0.3531973
# [2,] 0.4785452 0.71112122 0.1293723
# [3,] 0.8394404 0.05893438 0.7317925
# [4,] 0.8643395 0.45527445 0.7155661
它可以正确处理所有尺寸:

A[3,2,1,1]
# [1] 0.9446753
func(A, 3, 2, 1, 1)
# [1] 0.9446753
A[3,2,1,1,1]
# Error in A[3, 2, 1, 1, 1] : incorrect number of dimensions
func(A, 3, 2, 1, 1, 1)
# Error in func(A, 3, 2, 1, 1, 1) (from #4) : incorrect number of dimensions
以及尺寸过多的类似错误:

A[3,2,1,1]
# [1] 0.9446753
func(A, 3, 2, 1, 1)
# [1] 0.9446753
A[3,2,1,1,1]
# Error in A[3, 2, 1, 1, 1] : incorrect number of dimensions
func(A, 3, 2, 1, 1, 1)
# Error in func(A, 3, 2, 1, 1, 1) (from #4) : incorrect number of dimensions
编辑:以及我错过的部分。为了抓住空白,我们需要有一点乐趣

func <- function(ary, ..., drop = TRUE) {
  d <- length(dim(ary))
  dots <- as.list(match.call()[-(1:2)])
  if (length(dots) > d) stop("incorrect number of dimensions")
  pf <- parent.frame()
  dots <- lapply(seq_along(dots), function(i) {
    x <- dots[[i]]
    if (missing(x)) TRUE else eval(dots[[i]], env = pf)
  })
  rest <- rep(TRUE, d - length(dots))
  do.call(`[`, c(list(ary), c(dots, rest, drop = drop)))
}

如果给出了
A
,您可以从
dim(A)
(在您的案例5 4 3 2中)和
n=length(dim(A))
(在您的案例4中)中获取“一切”。你还需要什么?@Christoph我想他是在限制自己决定
n
,包括使用
length()
。这是对的@kjo?不清楚你想象传递给函数的是什么输入…无论如何,如果你为函数指定一个接口,函数本身会很自然地跟随,我想。。。例如,您可以有一个两列矩阵:一列用于dim索引(i_k中的下标),另一列用于选择(i_k中的值)。。。但实际上,这取决于你,应该在你的帖子中详细说明。@Christoph:我能回答你的问题的最好方法是提供以下练习:编写一个函数
f
,它以任意维数n>2的多维数组
a
作为参数,并返回所有(n-2)的列表-维子数组<代码> A[I,…,J] < /代码>,其中<代码> i < /> >和<代码> j>代码>范围内的所有有效下标为<代码> a <代码>。涵盖了吗?@r2evans:FWIW,Frank提到的案例在原始帖子中。是的,我现在看到了,30秒后修复好,所以30秒多一点。。。现在怎么样,kjo?谢谢。我从研究你的答案中学到了很多。我不知道
[
函数,也不知道可以混合布尔和整数下标(因此
TRUE
的行为类似于省略的下标)
A[3,2,1,1]
# [1] 0.9446753
func(A, 3, 2, 1, 1)
# [1] 0.9446753
A[3,2,1,1,1]
# Error in A[3, 2, 1, 1, 1] : incorrect number of dimensions
func(A, 3, 2, 1, 1, 1)
# Error in func(A, 3, 2, 1, 1, 1) (from #4) : incorrect number of dimensions
func <- function(ary, ..., drop = TRUE) {
  d <- length(dim(ary))
  dots <- as.list(match.call()[-(1:2)])
  if (length(dots) > d) stop("incorrect number of dimensions")
  pf <- parent.frame()
  dots <- lapply(seq_along(dots), function(i) {
    x <- dots[[i]]
    if (missing(x)) TRUE else eval(dots[[i]], env = pf)
  })
  rest <- rep(TRUE, d - length(dots))
  do.call(`[`, c(list(ary), c(dots, rest, drop = drop)))
}
d <- c(4, 2, 5, 4, 2, 7, 3)
set.seed(1)
A <- array(runif(prod(d)), dim = d)
A[3, 1, 4, , 1, 6, ]
#             [,1]      [,2]      [,3]
# [1,] 0.007668596 0.1818094 0.3278203
# [2,] 0.286473525 0.4119333 0.4825088
# [3,] 0.008869468 0.4767760 0.7649491
# [4,] 0.330141563 0.3438217 0.8710419
func(A, 3, 1, 4, , 1, 6)
#             [,1]      [,2]      [,3]
# [1,] 0.007668596 0.1818094 0.3278203
# [2,] 0.286473525 0.4119333 0.4825088
# [3,] 0.008869468 0.4767760 0.7649491
# [4,] 0.330141563 0.3438217 0.8710419
i <- 3
func(A, i, 1, 2+2, , 1, 6)
#             [,1]      [,2]      [,3]
# [1,] 0.007668596 0.1818094 0.3278203
# [2,] 0.286473525 0.4119333 0.4825088
# [3,] 0.008869468 0.4767760 0.7649491
# [4,] 0.330141563 0.3438217 0.8710419