I have a large data set 200 rows and 5 columns in a .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. So at the end I have 20 matrices each (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.