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

在R中构造动态大小的数组

在R中构造动态大小的数组,r,R,我想知道在R中构造动态大小数组的方法是什么 例如,我想构造一个n向量,但它的维数n是动态确定的。以下代码将起作用: > x=NULL > n=2; > for (i in 1:n) x[i]=i; > x [1] 1 2 再举一个例子,我想构造一个n乘2的矩阵,其中行数n是动态确定的。但我甚至无法指定第一行: > tmp=c(1,2) > x=NULL > x[1,]=tmp Error in x[1, ] = tmp

我想知道在R中构造动态大小数组的方法是什么

例如,我想构造一个n向量,但它的维数n是动态确定的。以下代码将起作用:

> x=NULL  
> n=2;   
> for (i in 1:n) x[i]=i;  
> x  
[1] 1 2  
再举一个例子,我想构造一个n乘2的矩阵,其中行数n是动态确定的。但我甚至无法指定第一行:

> tmp=c(1,2)  
> x=NULL  
> x[1,]=tmp  
Error in x[1, ] = tmp : incorrect number of subscripts on matrix  
> x[1,:]=tmp   
Error: unexpected ':' in "x[1,:"  
谢谢和问候

您可以通过以下方式找到它:

tmp = c(1,2) 

x = NULL

rbind(x, tmp)

我相信这是你需要的方法

arr <- array(1)

arr <- append(arr,3)

arr[1] <- 2

print(arr[1])

arr我们可以在填充数组后对其进行尺寸标注(以一维、向量、方式)
模拟问题的一维片段,下面是使用更高维度的方法

> x=c()
> tmp=c(1,2)
> n=6
> for (i in seq(1, by=2, length=n)) x[i:(i+1)] =tmp;
> dim(x) = c(2,n)
> x
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    1    1    1    1    1
[2,]    2    2    2    2    2    2
> 
与其使用
i:(i+1)
作为索引,不如使用
seq(i,length=2)
或者更好地使用
seq(i,length=length(tmp))
作为更通用的方法,如下所示(对于4 x 7阵列示例)

我们还可以通过使用cbind/rbind重新分配x来获得类似的结果,如下所示

> tmp=c(1,2)
> n=6
> x=rbind(tmp)
> for (i in 1:n) x=rbind(x, tmp);
> x
    [,1] [,2]
tmp    1    2
tmp    1    2
tmp    1    2
tmp    1    2
tmp    1    2
tmp    1    2
tmp    1    2
注意:使用

>dimnames(x)=NULL

当我想动态构造数组(矩阵)时,我会这样做:

n <- 500
new.mtrx <- matrix(ncol = 2, nrow = n)

head(new.mtrx)
     [,1] [,2]
[1,]   NA   NA
[2,]   NA   NA
[3,]   NA   NA
[4,]   NA   NA
[5,]   NA   NA
[6,]   NA   NA

我认为您正在寻找的答案是rbind()和cbind():

>x=NULL#也可以使用x rbind(x,c(1,2))
[,1] [,2]
[1,]    1    2
>x x x
[,1] [,2]
[1,]    1    2
[2,]    1    2
>x x
[,1] [,2] [,3]
[1,]    1    2    1
[2,]    1    2    2
在某些语言中可以尝试动态地分配“新索引”的策略,但在R中不能这样做

也可以使用矩阵包中提供的稀疏矩阵。它们将允许
M形式的赋值
n <- 500
new.mtrx <- matrix(ncol = 2, nrow = n)

head(new.mtrx)
     [,1] [,2]
[1,]   NA   NA
[2,]   NA   NA
[3,]   NA   NA
[4,]   NA   NA
[5,]   NA   NA
[6,]   NA   NA
    matrix(letters, ncol = 2)
      [,1] [,2]
 [1,] "a"  "n" 
 [2,] "b"  "o" 
 [3,] "c"  "p" 
 [4,] "d"  "q" 
 [5,] "e"  "r" 
 [6,] "f"  "s" 
 [7,] "g"  "t" 
 [8,] "h"  "u" 
 [9,] "i"  "v" 
[10,] "j"  "w" 
[11,] "k"  "x" 
[12,] "l"  "y" 
[13,] "m"  "z" 
> x=NULL  #  could also use x <- c()

> rbind(x, c(1,2))
     [,1] [,2]
[1,]    1    2
> x <- rbind(x, c(1,2))
> x <- rbind(x, c(1,2))  # now extend row-wise
> x
     [,1] [,2]
[1,]    1    2
[2,]    1    2
> x <- cbind(x, c(1,2))  # or column-wise
> x
     [,1] [,2] [,3]
[1,]    1    2    1
[2,]    1    2    2
 require(Matrix)
 M <- sparseMatrix(i=200, j=50, x=234)
 M[1,1]
#   [1] 0
 M[200, 50]
#   [1] 234
n = 5
x = c(1,2) %o% rep(1,n)
x
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    1    1    1    1
# [2,]    2    2    2    2    2

x = rep(1,n) %o% c(1,2)
x
#      [,1] [,2]
# [1,]    1    2
# [2,]    1    2
# [3,]    1    2
# [4,]    1    2
# [5,]    1    2