0

I have a data frame (d) composed of 640 observations for 55 variables.

I would like to randomly sample this data frame in 10 sub data frame of 64 observations for 55 variables. I don't want any of the observation to be in more than one sub data-frame.

This code work for one sample

d1 <- d[sample(nrow(d),64,replace=F),]

How can I repeat this treatment ten times ?

This one give me a data-frame of 10 variables (each one is one sample...)

d1 <- replicate(10,sample(nrow(d),64,replace = F))}

Can anyone help me?

fer.trecool
  • 71
  • 1
  • 2
  • 9
ePoQ
  • 434
  • 3
  • 18

1 Answers1

1

Here's a solution that returns the result in a list of data.frames:

d <- data.frame(A=1:640, B=sample(LETTERS, 640, replace=TRUE)) # an exemplary data.frame
idx <- sample(rep(1:10, length.out=nrow(d)))
res <- split(d, idx)
res[[1]] # first data frame
res[[10]] # last data frame

The only tricky part involves creating idx. idx[i] identifies the resulting data.frame, idx[i] in {1,...,10}, in which the ith row of d will occur. Such an approach assures us that no row will be put into more than 1 data.frame.

Also, note that sample returns a random permutation of (1,2,...,10,1,2,...,10).

Another approach is to use:

apply(matrix(sample(nrow(d)), ncol=10), 2, function(idx) d[idx,])
gagolews
  • 12,836
  • 2
  • 50
  • 75
  • Thanks but I have a problem still. That's seems to work for a df of 1 variable but I have 55 – ePoQ May 23 '14 at 16:14
  • I will try it another way since I really am a beginer in R. I have a dataframe of 640 lines and 55 columns called d. I want to divide this dataframe in 10 subset of 64 lines and 55 columns. But I need the repartition of the lines in the subsets to be randomized. – ePoQ May 23 '14 at 16:24
  • Okay my bad so. Since I am a beginner I have problem to assign the 10 results in ten differents df. Thanks for the code, I will try to manage it! – ePoQ May 23 '14 at 16:30
  • I've made an edit - explains how to access each resulting data frame. HTH. BTW, here's an [R intro](http://cran.r-project.org/doc/manuals/r-release/R-intro.html) -- good for start – gagolews May 23 '14 at 16:36
  • That's just perfect ! thanks for the edit and your time – ePoQ May 23 '14 at 17:07