Skip to content

Random subset

6 messages · Alessandro, John Kane, Marc Schwartz +1 more

#
?sample
--- On Wed, 7/30/08, Alessandro <alessandro.montaghi at unifi.it> wrote:

            
__________________________________________________________________
[[elided Yahoo spam]]
#
Hi,

check
?sample

n <- 200
mydata.set <- rnorm(100000)
my.random.subset <- sample(x=mydata.set, size=n, replace=TRUE)

my.random.subset


I hope this helps,
Roland
Alessandro wrote:
#
on 07/30/2008 01:18 PM Alessandro wrote:
See ?sample.

Randomly select 10 elements from a 30 element vector:

Vec <- 1:30

 > sample(Vec, 10)
  [1] 16  5 10 29 27 30 21  1 12 28


Randomly select 5 rows from a 10 row matrix:

MAT <- matrix(1:20, 10, 2)

 > MAT
       [,1] [,2]
  [1,]    1   11
  [2,]    2   12
  [3,]    3   13
  [4,]    4   14
  [5,]    5   15
  [6,]    6   16
  [7,]    7   17
  [8,]    8   18
  [9,]    9   19
[10,]   10   20


 > MAT[sample(5), ]
      [,1] [,2]
[1,]    5   15
[2,]    3   13
[3,]    4   14
[4,]    1   11
[5,]    2   12


HTH,

Marc Schwartz
#
on 07/30/2008 01:28 PM Marc Schwartz wrote:
Sorry, correction, that should be:

 > MAT[sample(10, 5), ]
      [,1] [,2]
[1,]    8   18
[2,]   10   20
[3,]    1   11
[4,]    6   16
[5,]    3   13


Fingers too fast there...

Marc
#
But probably you will need to set replace=FALSE if you want to sample 
from your original data set without replacement.

Sorry for the possible confusion,
Roland
Roland Rau wrote: