? stato filtrato un testo allegato il cui set di caratteri non era indicato... Nome: non disponibile URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20080730/c69d6903/attachment.pl>
Random subset
6 messages · Alessandro, John Kane, Marc Schwartz +1 more
?sample
--- On Wed, 7/30/08, Alessandro <alessandro.montaghi at unifi.it> wrote:
From: Alessandro <alessandro.montaghi at unifi.it> Subject: [R] Random subset To: r-help at r-project.org Received: Wednesday, July 30, 2008, 2:18 PM Hi all, I wish to do a random subset (i.e. 200 or 300 points) from a dataset, but I don't find the right code in R. Thanks for help alessandro [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
__________________________________________________________________ [[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:
Hi all, I wish to do a random subset (i.e. 200 or 300 points) from a dataset, but I don't find the right code in R. Thanks for help alessandro [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
on 07/30/2008 01:18 PM Alessandro wrote:
Hi all, I wish to do a random subset (i.e. 200 or 300 points) from a dataset, but I don't find the right code in R.
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:
on 07/30/2008 01:18 PM Alessandro wrote:
Hi all, I wish to do a random subset (i.e. 200 or 300 points) from a dataset, but I don't find the right code in R.
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
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:
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:
Hi all,
I wish to do a random subset (i.e. 200 or 300 points) from a dataset,
but I
don't find the right code in R.
Thanks for help
alessandro
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.