简体   繁体   English

将索引分配给r中的for循环内的矩阵

[英]assigning index to a matrix inside for-loop in r

I have a large data set 200 rows and 5 columns in a .CSV fromat. 我有一个大数据集,其中200个行和5列位于.CSV fromat中。 here is part of data set: 这是数据集的一部分:

 4.1    1.2 47.3    10954   51
 3.4    1.5 0.5 1   5316
 0.3    30.1    1.2 10  875
 0.2    0.4 119 0   0
   0    52.6    0.1 0   3.1
   0    0.3 880 0   0
   0    0.1 148 180 0
   0    0.1 490.2   0   0.4
   0    1.1 0.2 0.6 0.9
   0    0   0   0   0

I want to write a code to read each 10 rows separately and store it in a matrix(10 by 5) using for-loop. 我想编写一个代码以分别读取每10行,并使用for循环将其存储在矩阵(10 x 5)中。 So at the end I have 20 matrices each (10*5). 所以最后我每个都有20个矩阵(10 * 5)。 This is the command line: 这是命令行:

all.data   <- read.csv("C:\\Users\\Desktop\\myarray.csv",header=FALSE)#read whole data
for (k in 1:20){   
data_temp.k <- array(NA, dim=c(10,5))
  for( i in 1:10 ){
    for( j in 1:5 ) {
        data_temp.k[i,j] <- all.data[(k-1)*10:k*10,j]
    }
  }
}
write.csv(data_temp.k,"mymatrix.k")

I know the problem is somehow related to "k" and its dual function here as both matrix index and counter. 我知道问题某种程度上与“ k”有关,在这里它的双重功能既是矩阵索引又是计数器。

Don't use a loop for this, use row indexing : 不要为此使用循环,而是使用行索引:

## Sample data
set.seed(1)
m <- matrix(rnorm(1000),nrow=200,ncol=5)
## Generate indices to keep
indices <- seq(1,nrow(m), by=10)
## Subset matrix rows
m[indices,]

This probably doesn't add much other than being a nice demonstration of how you can use array s and aperm to split a mtrix into chunks and reshape, all using base R vectorised functions. 除了很好地演示如何使用array s和apermaperm拆分为块并重塑(全部使用base R向量化函数)外,这可能没有多大好处。 You can always apply functions to each dimension of an array using apply . 您可以随时申请功能,使用阵列的每个维度apply

#  Sample data
m <- matrix( 1:16 , 4 , 4 )
#     [,1] [,2] [,3] [,4]
#[1,]    1    5    9   13
#[2,]    2    6   10   14
#[3,]    3    7   11   15
#[4,]    4    8   12   16

# Use array() to turn into arrays and aperm() to transpose the 3D array t0 the result you expect
out <- aperm( array( t(m) , c(4,2,2) ) , c(2,1,3) )
#, , 1
#     [,1] [,2] [,3] [,4]
#[1,]    1    5    9   13
#[2,]    2    6   10   14

#, , 2
#     [,1] [,2] [,3] [,4]
#[1,]    3    7   11   15
#[2,]    4    8   12   16

You can apply functions over the third dimension, eg using 'apply' 您可以在第三维上应用函数,例如使用“ apply”

#  Sum all the elements in each of the third dimension of your arrays
apply( out , 3 , sum )
#[1] 60 76

If, though, you insist on using a for loop, you can -at least- use only one and not three nested loops. 但是,如果您坚持使用for循环,则至少可以使用一个而不是三个嵌套循环。

You don't need j because you want to keep all columns in each matrix. 您不需要j因为您希望将所有列保留在每个矩阵中。 Eg mat[1,] selects all columns and row 1; 例如mat[1,]选择所有列和第1行; you don't need to mat[1,1:ncol(mat)] . 您不需要mat[1,1:ncol(mat)]

Also, the way you use i is unneccessary, because you subset more than one row (using k-1 * 10 etc) to pass to row i every time. 另外,不需要使用i的方式,因为您会子集多于一行(使用k-1 * 10等)来每次都传递给第i行。

Finally, if you're trying to save each of the 20 matrices, you might need paste . 最后,如果您要保存20个矩阵中的每个矩阵,则可能需要paste

This should work (not tested): 这应该工作(未经测试):

for(k in 1:20)
 {
  data_temp.k <- all.data[((k-1)*10):(k*10),]

  write.csv(data_temp.k, paste("mymatrix", k, sep = ".")
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM