R使用矩阵来选择多维数组的行 [英] R use matrix to select row of multidimensional array

查看:151
本文介绍了R使用矩阵来选择多维数组的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组,按以下方式创建:

I have a multidimensional array, created in the following way:

my_array <- array(seq(12), dim=my_dims, dimnames=my_dimnames)

其中my_dims是一个数字向量,my_dimnames是一个字符向量列表,其长度对应于mydims中指定的维度,例如:

where "my_dims" is a numeric vector and "my_dimnames" is a list of character vectors, with lengths corresponding to the dimensions specified in "mydims", for example:

my_dims <- c(2, 3, 2)
my_dimnames <- list(c("D_11", "D_12"), c("D_21", "D_22", "D_23"), c("D_31", "D_32"))

因此my_array看起来像这样:

So that "my_array" looks something like this:

, , D_31

     D_21 D_22 D_23
D_11    1    3    5
D_12    2    4    6

, , D_32

     D_21 D_22 D_23
D_11    7    9   11
D_12    8   10   12

此外,我有一个包含每个维度的特定值的字符向量:

Additionally, I have a character vector containing specific values of each dimension:

my_values <- c("D_11", "D_21", "D_32")

现在,从这个问题: R - 如何通过索引向量获取多维数组的值 I我知道我可以使用矩阵索引从my_array中读取和写入特定单元格的值,如下所示:

Now, from this question: R - how to get a value of a multi-dimensional array by a vector of indices I learned that I can read and write values from and to specific cells in "my_array" using matrix indexing, like this:

my_array[matrix(my_values, 1)]

获取值7和

my_array[matrix(my_values, 1)] <- 1

将相同的单元格设置为值1

to set that same cell to value "1"

但我不明白的是,我是如何得到所有来自my_array的特定维度的值,给定使用此方法的所有其他维度的特定值。

What I don't understand, though, is how I can get all values from a specific dimension from "my_array", given specific values for all other dimensions using this method.

例如:如何获取包含所有其他值的向量第一维有固定值D_21和D_32第二维和第三维?所以在这种情况下,我尝试提取的是一个形式的向量:

For example: how can I get a vector containing all values of the first dimension with fixed values "D_21" and "D_32" for the second and third dimension? So in this case what I try to extract would be a vector of the form:

c(7, 8)

如何调整矩阵索引以达到此结果?

How do I need to adjust the matrix indexing to achieve this result?

非常感谢提前!

推荐答案

我不知道这样的事情是否已经存在,但是在评论中提到,您可以使用数组 dimnames

I don't know if something like this already exists, but as mentioned in the comments, you can make use of the dimnames of your array.

最好是你的my_values对象是一个列表,因为这会给你很多灵活性,你不会有向量。

It would be best if your "my_values" object were a list, as that would give you a lot of flexibility that you won't have with a vector.

这是一种可能的方法。创建一个创建矩阵的函数,该函数可用于从数组中进行子集化。该函数基本上看起来像这样:

Here's a possible approach. Create a function that creates a matrix that can be used to subset from your array. The function would essentially look something like this:

mat_maker <- function(array, vals) {
  x <- sapply(vals, is.null)
  vals[x] <- dimnames(array)[x]
  as.matrix(expand.grid(vals))
}

提取功能就像这样。 (最有可能的是,你只是将这两个功能一起推出,但我想我会在这里单独分享它们,以便你可以看到发生了什么。)

The extraction function would be like this. (Most likely, you would just roll both of these functions together, but I thought I would share them here separately so that you can see what is going on.)

array_extractor <- function(array, vals) {
  array[mat_maker(array, vals)]
}

现在,请尝试以下示例。请注意,vals参数是列表,而 NULL 用于未指定的维度。

Now, try the following examples out. Notice that the "vals" argument is a list, and that NULL is used for dimensions that are not being specified.

## All values where rowname == "D_11"
mat_maker(my_array, list("D_11", NULL, NULL))
#      Var1   Var2   Var3  
# [1,] "D_11" "D_21" "D_31"
# [2,] "D_11" "D_22" "D_31"
# [3,] "D_11" "D_23" "D_31"
# [4,] "D_11" "D_21" "D_32"
# [5,] "D_11" "D_22" "D_32"
# [6,] "D_11" "D_23" "D_32"
array_extractor(my_array, list("D_11", NULL, NULL))
# [1]  1  3  5  7  9 11


## All first column values from the second array object
## This is your specific example
mat_maker(my_array, list(NULL, "D_21", "D_32"))
#      Var1   Var2   Var3  
# [1,] "D_11" "D_21" "D_32"
# [2,] "D_12" "D_21" "D_32"
array_extractor(my_array, list(NULL, "D_21", "D_32"))
# [1] 7 8

## First two columns from all array dimensions
array_extractor(my_array, list(NULL, c("D_21", "D_22"), NULL))
# [1]  1  2  3  4  7  8  9 10






此函数的扩展是将维度属性(包括名称)放回输出中。它看起来像这样:


An extension of this function would be to put the dimensional attributes (including the names) back into the output. It could look something like this:

extractMe <- function(array, vals) {
  x <- sapply(vals, is.null)
  vals[x] <- dimnames(array)[x]
  temp <- as.matrix(expand.grid(vals))
  `dimnames<-`(`dim<-`(array[temp], lengths(vals)), vals)
}

以下是用法:

extractMe(my_array, list("D_11", NULL, NULL))
# , , D_31
# 
#      D_21 D_22 D_23
# D_11    1    3    5
# 
# , , D_32
# 
#      D_21 D_22 D_23
# D_11    7    9   11

extractMe(my_array, list(NULL, "D_21", "D_32"))
# , , D_32
# 
#      D_21
# D_11    7
# D_12    8

extractMe(my_array, list(NULL, c("D_21", "D_22"), NULL))
# , , D_31
# 
#      D_21 D_22
# D_11    1    3
# D_12    2    4
# 
# , , D_32
# 
#      D_21 D_22
# D_11    7    9
# D_12    8   10

而且,对于你只想要一个值向量的情况,只需用 c 包装它:

And, for cases where you want just a vector of the values, just wrap it with c:

c(extractMe(my_array, list(NULL, "D_21", "D_32")))
# [1] 7 8

这篇关于R使用矩阵来选择多维数组的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆