我有一个包含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 10020The 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 20The 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 20Applied 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更多推荐
发布评论