小编Luk*_*szJ的帖子

获取矩阵行作为矩阵

从矩阵中获取行作为矩阵的最短路径是什么?

> 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)

r matrix

6
推荐指数
1
解决办法
333
查看次数

附加函数名称但参数较少

我需要为my_function(i,x)创建其他名称(其中i可以是1到25之间的整数).我希望它像这样工作

  • my_function1(x)与my_function(1,x)相同
  • my_function2(x)与my_function(2,x)相同
  • my_function3(x)与my_function(3,x)相同
  • ...
  • my_function25(x)与my_function(25,x)相同

实现这一目标的一种方法是:

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通过引用传递,最后结果是

  • my_function1(x)与my_function(25,x)相同
  • my_function2(x)与my_function(25,x)相同
  • my_function3(x)与my_function(25,x)相同
  • ...

我怎样才能通过值传递"i"?或许还有其他方式......

我为什么要这样做?我在效率方面改进了其他R包,但同时我需要它与旧版本兼容.

r function assign

5
推荐指数
1
解决办法
92
查看次数

标签 统计

r ×2

assign ×1

function ×1

matrix ×1