[R] Sampling problems

Petr Savicky savicky at cs.cas.cz
Wed Mar 7 21:24:51 CET 2012


On Wed, Mar 07, 2012 at 08:41:35AM -0800, Oritteropus wrote:
> Hi,
> I need to sample randomly my dataset for 1000 times. The sample need to be
> the 80%. I know how to do that, my problem is that not only I need the 80%,
> but I also need the corresponding 20% each time. Is there any way to do
> that?

Hi.

If you use sample() to get the 80% and store the indices, you
can also get the remaining cases

  a <- matrix(1:30, ncol=3)
  i <- sample(10, 8)
  a[sort(i), ]

       [,1] [,2] [,3]
  [1,]    1   11   21
  [2,]    2   12   22
  [3,]    3   13   23
  [4,]    4   14   24
  [5,]    6   16   26
  [6,]    7   17   27
  [7,]    8   18   28
  [8,]   10   20   30

  a[-i, ]

       [,1] [,2] [,3]
  [1,]    5   15   25
  [2,]    9   19   29

Hope this helps.

Petr Savicky.



More information about the R-help mailing list