Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Matrix - Fatal编程技术网

R:将结果矩阵附加到环境中的所有列表

R:将结果矩阵附加到环境中的所有列表,r,list,matrix,R,List,Matrix,(我会先在聊天时问,但我的业力还不够。) 我有包含矩阵的列表,比如: List1 <- list(ch1 = matrix(1:4, ncol = 2), ch2 = matrix(5:8, ncol = 2)) List2 <- list(ch1 = matrix(2:5, ncol = 2), ch2 = matrix(6:9, ncol = 2)) 由于列表的长度不同,我必须按名称、ch1等来处理矩阵 我的方法得到了正确的结果,但不是作为矩阵追加,而是作为整数追加。解决这个问

(我会先在聊天时问,但我的业力还不够。)

我有包含矩阵的列表,比如:

List1 <- list(ch1 = matrix(1:4, ncol = 2), ch2 = matrix(5:8, ncol = 2))
List2 <- list(ch1 = matrix(2:5, ncol = 2), ch2 = matrix(6:9, ncol = 2))
由于列表的长度不同,我必须按名称、ch1等来处理矩阵

我的方法得到了正确的结果,但不是作为矩阵追加,而是作为整数追加。解决这个问题很麻烦,所以肯定有更好的方法

LST <- objects(pattern = "List+(\\d+)", envir = globalenv())

for (i in 1:length(LST)){
  pointer1 <- which(names(get(LST[[i]])) == "ch1")
  pointer2 <- which(names(get(LST[[i]])) == "ch2")

  matrix1 <- get(LST[[i]])[pointer1][[1]]
  matrix2 <- get(LST[[i]])[pointer2][[1]]

  result <- matrix1 + matrix2
  # print(dim(result))# results in 2 2, as expected

  assign(LST[[i]], c(get(LST[[i]]), matrix(result, ncol = ncol(matrix1))))

  # rm(pointer1, pointer2, matrix1, matrix2, result)
}

LST我已经解决了。由于某种原因,它只能像这样工作,因此必须以比预期更多的步骤编写代码。考虑以下事项:

List1 <- list(ch1 = matrix(1:4, ncol = 2), ch2 = matrix(5:8, ncol = 2))
List2 <- list(ch1 = matrix(2:5, ncol = 2), ch2 = matrix(6:9, ncol = 2))

LST <- objects(pattern = "List+(\\d+)", envir = globalenv())

for (i in 1:length(LST)){
  pointer1 <- which(names(get(LST[[i]])) == "ch1")
  pointer2 <- which(names(get(LST[[i]])) == "ch2")

  matrix1 <- get(LST[[i]])[pointer1][[1]]
  matrix2 <- get(LST[[i]])[pointer2][[1]]

  result <- matrix1 + matrix2

  tmpList <- get(LST[[i]])

  ListLength <- length(tmpList)
  chNames <- names(tmpList)
  chNames <- c(chNames, "ch3")

  tmpList[[ListLength + 1]] <- result

  names(tmpList) <- chNames

  assign(LST[[i]], tmpList)
}
rm(i, ListLength, LST, pointer1, pointer2, matrix1, matrix2, result, chNames, tmpList)
List1 <- list(ch1 = matrix(1:4, ncol = 2), ch2 = matrix(5:8, ncol = 2))
List2 <- list(ch1 = matrix(2:5, ncol = 2), ch2 = matrix(6:9, ncol = 2))

LST <- objects(pattern = "List+(\\d+)", envir = globalenv())

for (i in 1:length(LST)){
  pointer1 <- which(names(get(LST[[i]])) == "ch1")
  pointer2 <- which(names(get(LST[[i]])) == "ch2")

  matrix1 <- get(LST[[i]])[pointer1][[1]]
  matrix2 <- get(LST[[i]])[pointer2][[1]]

  result <- matrix1 + matrix2

  tmpList <- get(LST[[i]])

  ListLength <- length(tmpList)
  chNames <- names(tmpList)
  chNames <- c(chNames, "ch3")

  tmpList[[ListLength + 1]] <- result

  names(tmpList) <- chNames

  assign(LST[[i]], tmpList)
}
rm(i, ListLength, LST, pointer1, pointer2, matrix1, matrix2, result, chNames, tmpList)
> List1
$ch1
     [,1] [,2]
[1,]    1    3
[2,]    2    4

$ch2
     [,1] [,2]
[1,]    5    7
[2,]    6    8

$ch3
     [,1] [,2]
[1,]    6   10
[2,]    8   12

> List2
$ch1
     [,1] [,2]
[1,]    2    4
[2,]    3    5

$ch2
     [,1] [,2]
[1,]    6    8
[2,]    7    9

$ch3
     [,1] [,2]
[1,]    8   12
[2,]   10   14