首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >调用在R中的函数中在同一函数中创建的赋值变量

调用在R中的函数中在同一函数中创建的赋值变量
EN

Stack Overflow用户
提问于 2020-03-27 01:09:06
回答 1查看 40关注 0票数 0

我正在编写一个函数来解决数独谜题。此函数的一部分将用于将矩阵拆分为三个9x3矩阵。然后,在将矩阵重新加入到一个大矩阵之前,我将对每个矩阵执行操作。

在这一阶段,我希望我的这部分职能能做三件事:

  1. 将矩阵分解为三个矩阵
  2. name每个创建的矩阵
  3. 在相同的函数

中调用新的矩阵

然而,我正在为第三步而奋斗。我已经编写了一个函数,它将矩阵分成三个,命名每一个新的矩阵,如果我输入行envir = globalenv(),这个函数确实返回我的矩阵拆分为三个,9x3矩阵,每个矩阵都有它的标识符名。太棒了!

但是,我想在函数的下一部分中调用由函数的步骤1和2创建的新矩阵。在运行函数之前,我将不知道矩阵的名称,因为我希望代码可以用于许多矩阵,而不管大小如何。

当我只知道对象的名称是"mat_n“(n为整数)时,是否有一种方法来调用主函数中由assign函数创建的对象。

为了清晰起见,下面是我的代码的简化版本:

代码语言:javascript
运行
复制
m <- matrix(sample(c(0:9), 81, replace = T), ncol = 9, nrow = 9)

matrix_split <- function(x){

  i <- 1:length(x[, 1])
  a <- 1:sum(i %% 3 == 0) # Will be used to name the temporary matrices
  b <- which(i %% 3 == 0) # Will be used to identify where to split main matrix


  for(n in a){  # This is to create a number of smaller matrices depending on the
                # number multiples of 3 that are present in the length of the object.

    nam <- paste("mat_", n, sep = "") # Each new matrix will be named and numbered
                                      # using the assign function below:

    assign(nam, x[, c((b[a[n]] - (sum(i %% 3 == 0) - 1)) : b[a[n]])])

    # Not a very elegant way of using the loop to split the matrix into blocks of
    # three. b[a[n]] returns either 3, 6 or 9, and (sum(i %% == 3) -1 ) = 2. So this
    # will return x[, c(1:3)], x[, c(4:6)] and x[, c(7:9)], when spliting the matrix
    # into three.

    }

}


matrix_split(m)

我只要求调用由赋函数创建的对象的特定解决方案,以便在创建主函数之后在我的主函数中使用。这将是一项有用的技能,也是我编程知识中的一个空白(这一点一点也不广泛)。

这也不是分解矩阵的最好方法,我知道已经创建了一些包来解决Sudoku难题,但是我想写我自己的东西,没有比先做不好然后改进的更好的学习方法了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-27 01:15:56

使用lsparent.frame怎么样?

代码语言:javascript
运行
复制
mat_4 <- matrix(LETTERS[1:16],nrow=4)
test <- function(){
ls(parent.frame())[grep("mat_",ls(parent.frame()))]
}
test()
# [1] "mat_4"
get(test())
#      [,1] [,2] [,3] [,4]
# [1,] "A"  "E"  "I"  "M" 
# [2,] "B"  "F"  "J"  "N" 
# [3,] "C"  "G"  "K"  "O" 
# [4,] "D"  "H"  "L"  "P" 

或者,如果您想包括当前环境和每一个更高的级别,就会有sys.frame()

编辑

为了绕开知道对象名称的问题,也许将结果存储在列表的元素中是一个更好的计划。

代码语言:javascript
运行
复制
matrix_split <- function(x){
  i <- 1:length(x[, 1])
  a <- 1:sum(i %% 3 == 0) 
  b <- which(i %% 3 == 0)
  #initialize list
  result <- list()
  for(n in a){  
    # assign submatrix to element in the list
    result[[n]] <- x[, c((b[a[n]] - (sum(i %% 3 == 0) - 1)) : b[a[n]])]
    }
do.call(cbind,result)
}

matrix_split(m)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
 [1,]    6    4    2    9    1    2    8    0    4
 [2,]    8    5    5    8    6    1    3    7    8
 [3,]    4    7    1    8    3    6    6    0    6
 [4,]    3    0    5    0    6    3    2    3    9
 [5,]    0    9    7    7    0    1    5    3    2
 [6,]    0    8    8    9    9    8    4    9    8
 [7,]    6    0    2    9    9    2    4    8    9
 [8,]    6    9    6    4    8    1    2    1    1
 [9,]    8    4    6    8    5    0    9    5    9
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60878540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档