Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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 使用矩阵子集表_R_Subset - Fatal编程技术网

R 使用矩阵子集表

R 使用矩阵子集表,r,subset,R,Subset,我刚开始学习R,但在使用n维表my.table时遇到了一些问题,我想以各种方式对其进行子集划分。我有一个行矩阵m,它允许我用my.table[m]选择表中的单个单元格。现在,我想使用m来选择表格的整行(而不仅仅是一个单元格),方法是指示一个我不希望被切片的维度I。这可以通过将索引的i-th位置保留为空来轻松完成,但在我的例子中,i的值只有在运行时才知道 我将尝试用一些代码来说明这一点: # let's assume n = 4 # I can pick a cell value with the

我刚开始学习R,但在使用n维表
my.table
时遇到了一些问题,我想以各种方式对其进行子集划分。我有一个行矩阵
m
,它允许我用
my.table[m]
选择表中的单个单元格。现在,我想使用
m
来选择表格的整行(而不仅仅是一个单元格),方法是指示一个我不希望被切片的维度
I
。这可以通过将索引的
i
-th位置保留为空来轻松完成,但在我的例子中,
i
的值只有在运行时才知道

我将尝试用一些代码来说明这一点:

# let's assume n = 4
# I can pick a cell value with the literal indexing
a.value <- my.table[2, 1, 4, 2]
# And I can reproduce this with matrix indexing
m <- matrix(c(2, 1, 4, 2), nrow=1)
a.value <- my.table[m]

# Now let's assume i = 3
# I can subset the table with literal indexing to pick the whole row
a.row <- my.table[2, 1, , 2]
# But how can I do the same using the matrix m?
更新3:


我发现我所说的“神奇”值实际上是存在的,你可以通过
bquote()
获得它-只是我认为没有办法将它与矩阵中的其他数字一起使用以形成适当的索引。

如何编写例如
my.table[1,1,1,1:2]
而不是
my.table[1,1,1,]
? 您可以将要扩展的索引设置为0(在
m
中),然后定义

f <- function(my.table, m) {
    dims <- dim(my.table)
    a <- lapply(1:4, function(x) if (m[, x] == 0) 1:dims[x] else m[, x])
    my.table[a[[1]], a[[2]], a[[3]], a[[4]]]
}

f尝试
m[i,]@javlacalle不,实际上
m
只是一行。我希望能够在
my.table
my中选择一行,为表中除
I
以外的每个维度提供具体值。我将编辑问题以更好地解释它。请发布
dput(my.table)
的输出以及您要选择的值。如果我发布
f(my.table,m,2)
我会收到一个错误,因为在使用
apply
时未定义参数
MARGIN
。如何使用此函数获得结果?您可以将所需的表达式定义为字符,然后对其求值。例如,
eval(parse(text=“my.table[2,1,2]”)
。在这种情况下,您只需要担心如何定义字符串。我想知道该语言是否提供了一些构造,以便在没有任何帮助函数的情况下直接执行该操作,但我假设它没有。你的解决方案很好,所以我接受它作为正确答案。
structure(c(0.830412306140461, 0.169587693859539, 0.944833625218914, 
0.0551663747810858, 0.993589743589744, 0.00641025641025641, 1, 
0, 0.992307692307692, 0.00769230769230769, 1, 0, 1, 0, NaN, NaN
), class = "table", .Dim = c(2L, 2L, 2L, 2L), .Dimnames = structure(list(
    V29.0 = c("0", "1"), `779.89` = c("0", "1"), `771.81` = c("0", 
    "1"), `771.89` = c("0", "1")), .Names = c("V29.0", "779.89", 
"771.81", "771.89")))
f <- function(my.table, m) {
    dims <- dim(my.table)
    a <- lapply(1:4, function(x) if (m[, x] == 0) 1:dims[x] else m[, x])
    my.table[a[[1]], a[[2]], a[[3]], a[[4]]]
}
m <- matrix(c(0, 1, 1, 0), nrow = 1)
my.table[, 1, 1, ] # same as my.table[1:2, 1, 1, 1:2]
#      771.89
# V29.0         0           1
#     0 0.8304123 0.992307692
#     1 0.1695877 0.007692308

f(my.table, m)
#      771.89
# V29.0         0           1
#     0 0.8304123 0.992307692
#     1 0.1695877 0.007692308