[R] Sample

(Ted Harding) Ted.Harding at manchester.ac.uk
Sat Jul 26 23:26:56 CEST 2008


On 26-Jul-08 21:12:56, Raphael Saldanha wrote:
> Hi!
> 
> I have a data.frame, like this one:
>> data
>   0-100 100-250 250+
> a     9      18   16
> b    33      25   27
> c    21      19   17
> d    25      22   19
> e    40      39   19
> f    77      45   29
> 
> If I request a sample of 2, I have the following:
> 
>> sample(data, 2)
>   0-100 250+
> a     9   16
> b    33   27
> c    21   17
> d    25   19
> e    40   19
> f    77   29
> 
> How can I request a sample from the rows???

Something like

  data[sample((1:nrow(data)),2),]

to get the samples rows in random order, or

  data[sort(sample((1:nrow(data)),2)),]

if you want just a random subset (in the original order).

(only: don't call your dataframe "data" -- that can cause problems.
Use some other name.)

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 26-Jul-08                                       Time: 22:26:54
------------------------------ XFMail ------------------------------



More information about the R-help mailing list