我有一長串嵌套陣列(長 60 個元素),我想減去另一個嵌套陣列串列(長 60 個元素)。這里的問題是第一個串列中的元素與第二個串列中的元素具有不同的維度。這是一個精簡/簡化的示例。我有類似的串列example1,example2但我的都是不同的(非重復)數字:
#create example1
set.seed(1)
example1.1 <- list(array(rnorm(18), dim = c(6,3, 10)))
example1.2 <- list(array(rnorm(12), dim = c(4,3, 10)))
example1 <- list(example1.1, example1.2)
#create example2
set.seed(2)
example2.1 <- list(array(rnorm(18), dim = c(6,3, 1)))
example2.2 <- list(array(rnorm(12), dim = c(4,3, 1)))
example2 <- list(example2.1, example2.2)
基本上,我想在每個不同的高度位置從example1.1中減去example2.1,然后從example1.2中減去example2.2。如果輸出也是一個陣列串列,那就太好了:
[[1]]
[[1]][[1]]
, , 1
[,1] [,2] [,3]
[1,] 0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586 0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,] 2.72565648 -0.1666014 2.2661355
[5,] 0.40975953 1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095 0.9080295
, , 2
[,1] [,2] [,3]
[1,] 0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586 0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,] 2.72565648 -0.1666014 2.2661355
[5,] 0.40975953 1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095 0.9080295
, , 3
[,1] [,2] [,3]
[1,] 0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586 0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,] 2.72565648 -0.1666014 2.2661355
[5,] 0.40975953 1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095 0.9080295
, , 4
[,1] [,2] [,3]
[1,] 0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586 0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,] 2.72565648 -0.1666014 2.2661355
[5,] 0.40975953 1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095 0.9080295
, , 5
[,1] [,2] [,3]
[1,] 0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586 0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,] 2.72565648 -0.1666014 2.2661355
[5,] 0.40975953 1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095 0.9080295
, , 6
[,1] [,2] [,3]
[1,] 0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586 0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,] 2.72565648 -0.1666014 2.2661355
[5,] 0.40975953 1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095 0.9080295
, , 7
[,1] [,2] [,3]
[1,] 0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586 0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,] 2.72565648 -0.1666014 2.2661355
[5,] 0.40975953 1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095 0.9080295
, , 8
[,1] [,2] [,3]
[1,] 0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586 0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,] 2.72565648 -0.1666014 2.2661355
[5,] 0.40975953 1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095 0.9080295
, , 9
[,1] [,2] [,3]
[1,] 0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586 0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,] 2.72565648 -0.1666014 2.2661355
[5,] 0.40975953 1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095 0.9080295
, , 10
[,1] [,2] [,3]
[1,] 0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586 0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,] 2.72565648 -0.1666014 2.2661355
[5,] 0.40975953 1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095 0.9080295
[[2]]
[[2]][[1]]
, , 1
[,1] [,2] [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478 0.4824913 0.71957790
[4,] 2.7952066 3.1900311 0.10020653
, , 2
[,1] [,2] [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478 0.4824913 0.71957790
[4,] 2.7952066 3.1900311 0.10020653
, , 3
[,1] [,2] [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478 0.4824913 0.71957790
[4,] 2.7952066 3.1900311 0.10020653
, , 4
[,1] [,2] [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478 0.4824913 0.71957790
[4,] 2.7952066 3.1900311 0.10020653
, , 5
[,1] [,2] [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478 0.4824913 0.71957790
[4,] 2.7952066 3.1900311 0.10020653
, , 6
[,1] [,2] [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478 0.4824913 0.71957790
[4,] 2.7952066 3.1900311 0.10020653
, , 7
[,1] [,2] [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478 0.4824913 0.71957790
[4,] 2.7952066 3.1900311 0.10020653
, , 8
[,1] [,2] [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478 0.4824913 0.71957790
[4,] 2.7952066 3.1900311 0.10020653
, , 9
[,1] [,2] [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478 0.4824913 0.71957790
[4,] 2.7952066 3.1900311 0.10020653
, , 10
[,1] [,2] [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478 0.4824913 0.71957790
[4,] 2.7952066 3.1900311 0.10020653
我懷疑我根本不知道如何適當地對回圈進行子集化。任何人都可以提供的任何幫助都非常感謝!謝謝!
uj5u.com熱心網友回復:
也許是這樣的?:
Results.1 <- list(array(NA, dim = c(6,3,10)))
for(i in 1:10){
Results.1[[1]][,,i] <- example1.1[[1]][,,i]-example2.1[[1]][,,1]
}
對于 example1 串列:
Results[[1]] = list(array(NA, dim = c(6,3, 10)))
Results[[2]] = list(array(NA, dim = c(4,3, 10)))
for(i in 1:length(example1)){
for(j in 1:10){
Results[[i]][[1]][,,j] <- example1[[i]][[1]][,,j]-example2[[i]][[1]][,,1]
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/318539.html
下一篇:根據R中的變數名規則創建新變數
