[R] Sampling from data set

Jeffrey Spies jspies at virginia.edu
Tue Oct 5 10:34:53 CEST 2010


If poproh.3 was your dataset as a data.frame (an object with row and
column dimensions), you need a comma following the row selection
(sample(...)) to indicate that you want to select those rows and all
columns:

newsample <-poprho.3[sample(1:10000,100),] # note the last comma in the brackets

General use is:

my.data.frame[rows,columns]

Where either rows or columns (or both) can be left blank to indicate
that you want all of them.  Similarly, a selection of the first column
would have been (comma followed by column number):

newsample <-poprho.3[sample(1:10000,100),1]

That's why your:

newsample <-as.matrix(nameofdataset[sample(1:10000,100),])

worked; the as.matrix wasn't necessary to simply sample the data.

Cheers,

Jeff.

On Tue, Oct 5, 2010 at 3:54 AM, Jumlong Vongprasert
<jumlong.ubru at gmail.com> wrote:
> Dear Jeffrey.
> I used "newsample <-as.matrix(nameofdataset[sample(1:10000,100),])".
> Now it include all 2 variable.
> Thank you for your answer to inspire.
> Jumlong
>
> 2010/10/5 Jeffrey Spies <jspies at virginia.edu>
>>
>> We'll probably need much more info, but this should get you started:
>>
>> nameOfDataSet[sample(1:10000, 100),]
>>
>> You can replace the 10000 with dim(nameOfDataSet)[1] to make it more
>> dynamic.
>>
>> Jeff.
>>
>> On Tue, Oct 5, 2010 at 3:07 AM, Jumlong Vongprasert
>> <jumlong.ubru at gmail.com> wrote:
>> > Dear all.
>> > I have data with 2 variable x,y size 10000.
>> > I want to sampling from this data with size 100.
>> > How I can do it.
>> > THANK.
>> >
>> > --
>> > Jumlong Vongprasert
>> > Institute of Research and Development
>> > Ubon Ratchathani Rajabhat University
>> > Ubon Ratchathani
>> > THAILAND
>> > 34000
>> >
>> >        [[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.
>> >
>
>
>
> --
> Jumlong Vongprasert
> Institute of Research and Development
> Ubon Ratchathani Rajabhat University
> Ubon Ratchathani
> THAILAND
> 34000
>



More information about the R-help mailing list