これを試してください(さらなるニーズに合わせて調整できると確信しています):
shaper <- function(obs, a, b) {
array(obs, dim=c(a, b, length(obs)/a/b))
}
shaper(obs, 100, 100)
必要なサイズが正しいかどうかわからない場合は、次のように、残り物があるかどうかを確認するチェックを含めることができます。
shaper <- function(obs, a, b) {
dimension <- length(obs)/a/b
if (dimension %% 1 != 0) {
stop("not correctly divisible")
}
else {
return(array(obs, dim=c(a, b, dimension)))
}
}
shaper(obs, 100, 100)
入力として任意の量の次元を与える機能も追加され、1 だけ拡張しようとします。
shaper <- function(obs, ...) {
len.remaining <- length(obs)
for (i in c(...)) {
len.remaining <- len.remaining / i
}
if (len.remaining %% 1 != 0) {
stop("not correctly divisible")
}
else {
return(array(obs, dim=c(..., len.remaining)))
}
}
これで、次を使用できるようになります。
obs <- rep(1, 100 * 100 * 5)
> res <- shaper(obs, 100, 100)
> dim(res)
[1] 100 100 5
> res <- shaper(obs, 10, 10, 100)
> dim(res)
[1] 10 10 100 5