值分配给阵列中的R特定维 [英] Assign values to a specific dimension of array in R

查看:152
本文介绍了值分配给阵列中的R特定维的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多维数组,并尝试将值分配给特定维度。请参阅codeS下面我目前的方法。

I have a multiple dimension array and try to assign values to a specific dimension. Please see codes below for my current method.

# Create a array and fill with NA
set.seed(1)
dim_arr <- seq(2, 10)
arr <- array(NA, dim = dim_arr)

# Add values to the dimension 2 and fill other dimensions
add_dim <- 2

# Generate value
values <- runif(prod(dim_arr[-add_dim]))
# Assign value to position 1 of dimension 2
arr[,1,,,,,,,] <- values

values <- runif(prod(dim_arr[-add_dim]))
# Assign value to position 2 of dimension 2
arr[,2,,,,,,,] <- values

我的问题是尺寸(dim_arr)和更改的尺寸(add_dim)的数量是不固定的。当参数dim_arr和add_dim发生变更,code常用3 [,1 ,,,,,,,]坏了,我每次都去改变它。

My problem is that the number of dimension (dim_arr) and changed dimension (add_dim) are not fixed. When parameters dim_arr and add_dim are changed, the code "arr[,1,,,,,,,]" is broken which I have to change it every time.

有没有改进我的codeS任何通用的方法?

Is there any generic method to improve my codes?

请让我知道如果我的问题不明确。感谢您的任何建议。

Please let me know if my question is not clear. Thanks for any suggestions.

推荐答案

要做到这一点,正确的方式将是调用 do.call 指定的严重程度每个维度比你想要的(如 do.call(一个其他的[&LT; - 列表(ARR,1:X,1:Y,3,价值观) )的有趣的方式是采取优势在 <一个href=\"http://stackoverflow.com/questions/3892580/create-missing-objects-aka-empty-symbols-empty-objects-needed-for-f\">mysterious丢失的对象 ,即可生成 x中的空参数[,,, ,,,ž]

The correct way to do this will be to a call to do.call specifying the full extent of each dimension other than the one you want (e.g. do.call("[<-", list(arr, 1:x, 1:y, 3, values)). The fun way is to take advantage of the mysterious missing object to generate the empty arguments in x[,,,z,,,].

编辑:一个比较无聊,但更简单的方法是包括在最后,因为它原来是一个true作为参数就相当于缺少的参数

a more boring but simpler approach is included at the end as it turns out a single TRUE as an argument is equivalent to the missing argument.

EDIT2:与哈德利的代替简化()办法

simplified with Hadley's substitute() approach.

replace_dim <- function(arr, rep.val, dim.to.rep, dim.val) {
  dim.list <- replicate(length(dim(arr)), substitute())
  dim.list[dim.to.rep] <- dim.val
  do.call(`[<-`, c(list(arr), dim.list, list(rep.val)))
}
arr <- array(1:8, dim=rep(2, 3))

replace_dim(arr, 555, 3, 1)  # note this doesn't modify `arr`, so you would have to set `arr` to the return value
# , , 1
#      [,1] [,2]
# [1,]  555  555
# [2,]  555  555
# , , 2
#      [,1] [,2]
# [1,]    5    7
# [2,]    6    8

replace_dim(arr, 555, 2, 1)
# , , 1
#      [,1] [,2]
# [1,]  555    3
# [2,]  555    4
# , , 2
#      [,1] [,2]
# [1,]  555    7
# [2,]  555    8

replace_dim(arr, 555, 1, 1)
# , , 1
#      [,1] [,2]
# [1,]  555  555
# [2,]    2    4
# , , 2
#      [,1] [,2]
# [1,]  555  555
# [2,]    6    8

replace_dim(arr, 555, 1, 2)
# , , 1
#      [,1] [,2]
# [1,]    1    3
# [2,]  555  555
# , , 2
#      [,1] [,2]
# [1,]    5    7
# [2,]  555  555  

此外,这里我们使用了 555 ,但你也可以使用任何向量/对象适合你插入,尺寸

Also, here we used 555, but you could have used any vector/object that fits the dimensions you are inserting into.

replace_dim_boring <- function(arr, rep.val, dim.to.rep, dim.val) {
  dim.list <- as.list(rep(T, length(dim(arr))))
  dim.list[dim.to.rep] <- dim.val
  do.call(`[<-`, c(list(arr), dim.list, list(rep.val)))
}

这篇关于值分配给阵列中的R特定维的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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