从矩阵中获取行作为矩阵的最短路径是什么?
> x<-matrix(1:9,nrow=3,byrow=TRUE)
> x
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> x[1,]
[1] 1 2 3
> is.vector(x[1,])
[1] TRUE
Run Code Online (Sandbox Code Playgroud)
在哪里,我想得到
[,1] [,2] [,3]
[1,] 1 2 3
Run Code Online (Sandbox Code Playgroud) 我需要为my_function(i,x)创建其他名称(其中i可以是1到25之间的整数).我希望它像这样工作
实现这一目标的一种方法是:
my_function1 <- function (x) my_function(1, x)
my_function2 <- function (x) my_function(2, x)
my_function3 <- function (x) my_function(3, x)
...
Run Code Online (Sandbox Code Playgroud)
但是因为它们中有25个,所以在循环中制作它是合理的.为此,我尝试过:
for(i in 1:25){
assign(paste("my_function",i,sep=""),function(x) my_function(i,x))
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为i通过引用传递,最后结果是
我怎样才能通过值传递"i"?或许还有其他方式......
我为什么要这样做?我在效率方面改进了其他R包,但同时我需要它与旧版本兼容.