0

I am new to R. I have to repeat some process several times. In each iteration, I have to take a sub-sample from a data-frame, and to save the output which already is an array as rows of a matrix. To take sub-sample I am using

mysample <- mydata[sample(1:nrow(mydata), 200,replace=T),]

and to save output in 'finmat' matrix

finmat<- matrix(nrow=100, ncol=7)  # initialization with 100 iteration

in each iteration suppose looping index is k, finmat[k,]<- rs where rs is vector of length 7. help me to accomplish this task.

user2554330
  • 37,248
  • 4
  • 43
  • 90
rgt
  • 31
  • 1
  • 8
  • 2
    I think you are being very unspecific when you talk about data frames - a subsample of a data frame is a data frame (not an array), and a matrix is a simpler object so it won't work to save the subset as "rows of a matrix". Everything will be clear if you make a reproducible example by sharing a little sample input data (use `dput(droplevels(head(your_data)))` so all the structural information is preserved) and also show a little bit of what your desired output would look like. [See here for a nice tutorial on creating reproducible examples in R](https://stackoverflow.com/q/5963269/903061). – Gregor Thomas Nov 20 '17 at 19:41
  • A little confused -- you want to take samples of 200 rows apiece, but you also want to store each sample as a single row of the output matrix... If that's right, then how are the 200 rows reduced to a single one? – lefft Nov 20 '17 at 23:41
  • head(mydata) [,1] [,2] [,3] [,4] [1,] 4 5 0 20 [2,] 2 2 1 29 [3,] 2 5 0 38 [4,] 4 5 0 28 [5,] 3 5 0 08 [6,] 2 4 1 25 suppose this is my data. using these columns, I calculate several values say v1, v2, v3, v4, v5. so at each iteration, I have to save these 5 values. and repeating this process after 100 time, I have to take mean of each five values. so what should I do?? – rgt Nov 22 '17 at 15:13

0 Answers0