如何在R中增加数组的大小(How to increase the size of an array in R)

编程入门 行业动态 更新时间:2024-10-25 08:26:24
如何在R中增加数组的大小(How to increase the size of an array in R)

我有一个包含10,000行和2列的2D数组A.

在第一个实例中,我想只使用数组A的前200行。我做了以下内容:New_array = A [A(1:200),]

每次我想要将行数增加50.即在第二次迭代中我想要访问250行矩阵A,第三次迭代300等等,直到我达到矩阵的原始大小。

我知道我必须创建一个for循环,但我很挣扎。 任何帮助将受到高度赞赏

I have a 2D array A with 10,000 rows and 2 columns.

At a first instance I want to use only the first 200 rows of the array A. I did the following: New_array=A[A(1:200) ,]

Each time I want to increase the number of the rows by 50. i.e. in the second iteration I want to have access on the 250 rows of matrix A, third iteration 300 and so on until I reach the original size of the matrix.

I know that I have to create a for loop but I struggle. Any help will be highly appreciated

最满意答案

seq函数允许您指定序列中的间隔,如@ db的注释中所示。

seq(0, 20, by = 5) [1] 0 5 10 15 20

然后seq的输出可用于驱动循环。 这里i被用作每次迭代中序列的端点。

for ( i in seq(5, 20, by = 5) ) { print(1:i) } [1] 1 2 3 4 5 [1] 1 2 3 4 5 6 7 8 9 10 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

应用于您的示例,序列可用于对矩阵进行子集化

# Example matrix m <- 10000 n <- 2 A <- matrix(1:(m*n), ncol = n) head(A) [,1] [,2] [1,] 1 10001 [2,] 2 10002 [3,] 3 10003 [4,] 4 10004 [5,] 5 10005 [6,] 6 10006 # Iterate with a loop jump <- 5 # I'm using 5 instead of 50 for ( i in seq(jump, m, by = jump) ) { print(paste("i =", i)) print( A[1:i, ] ) # subset the matrix if ( i > 15 ) break # limiting the output for readability } [1] "i = 5" [,1] [,2] [1,] 1 10001 [2,] 2 10002 [3,] 3 10003 [4,] 4 10004 [5,] 5 10005 [1] "i = 10" [,1] [,2] [1,] 1 10001 [2,] 2 10002 [3,] 3 10003 [4,] 4 10004 [5,] 5 10005 [6,] 6 10006 [7,] 7 10007 [8,] 8 10008 [9,] 9 10009 [10,] 10 10010 [1] "i = 15" [,1] [,2] [1,] 1 10001 [2,] 2 10002 [3,] 3 10003 [4,] 4 10004 [5,] 5 10005 [6,] 6 10006 [7,] 7 10007 [8,] 8 10008 [9,] 9 10009 [10,] 10 10010 [11,] 11 10011 [12,] 12 10012 [13,] 13 10013 [14,] 14 10014 [15,] 15 10015 [1] "i = 20" [,1] [,2] [1,] 1 10001 [2,] 2 10002 [3,] 3 10003 [4,] 4 10004 [5,] 5 10005 [6,] 6 10006 [7,] 7 10007 [8,] 8 10008 [9,] 9 10009 [10,] 10 10010 [11,] 11 10011 [12,] 12 10012 [13,] 13 10013 [14,] 14 10014 [15,] 15 10015 [16,] 16 10016 [17,] 17 10017 [18,] 18 10018 [19,] 19 10019 [20,] 20 10020

The seq function allows you to specify intervals in a sequence, as shown in @d.b's comment.

seq(0, 20, by = 5) [1] 0 5 10 15 20

The output of seq can then be used to drive the loop. Here i is used as the endpoint for a sequence in each iteration.

for ( i in seq(5, 20, by = 5) ) { print(1:i) } [1] 1 2 3 4 5 [1] 1 2 3 4 5 6 7 8 9 10 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Applied to your example, the sequences can be used to subset the matrix

# Example matrix m <- 10000 n <- 2 A <- matrix(1:(m*n), ncol = n) head(A) [,1] [,2] [1,] 1 10001 [2,] 2 10002 [3,] 3 10003 [4,] 4 10004 [5,] 5 10005 [6,] 6 10006 # Iterate with a loop jump <- 5 # I'm using 5 instead of 50 for ( i in seq(jump, m, by = jump) ) { print(paste("i =", i)) print( A[1:i, ] ) # subset the matrix if ( i > 15 ) break # limiting the output for readability } [1] "i = 5" [,1] [,2] [1,] 1 10001 [2,] 2 10002 [3,] 3 10003 [4,] 4 10004 [5,] 5 10005 [1] "i = 10" [,1] [,2] [1,] 1 10001 [2,] 2 10002 [3,] 3 10003 [4,] 4 10004 [5,] 5 10005 [6,] 6 10006 [7,] 7 10007 [8,] 8 10008 [9,] 9 10009 [10,] 10 10010 [1] "i = 15" [,1] [,2] [1,] 1 10001 [2,] 2 10002 [3,] 3 10003 [4,] 4 10004 [5,] 5 10005 [6,] 6 10006 [7,] 7 10007 [8,] 8 10008 [9,] 9 10009 [10,] 10 10010 [11,] 11 10011 [12,] 12 10012 [13,] 13 10013 [14,] 14 10014 [15,] 15 10015 [1] "i = 20" [,1] [,2] [1,] 1 10001 [2,] 2 10002 [3,] 3 10003 [4,] 4 10004 [5,] 5 10005 [6,] 6 10006 [7,] 7 10007 [8,] 8 10008 [9,] 9 10009 [10,] 10 10010 [11,] 11 10011 [12,] 12 10012 [13,] 13 10013 [14,] 14 10014 [15,] 15 10015 [16,] 16 10016 [17,] 17 10017 [18,] 18 10018 [19,] 19 10019 [20,] 20 10020

更多推荐

本文发布于:2023-07-29 20:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319539.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   大小   如何在   array   size

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!