Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sample random rows in dataframe

I am struggling to find the appropriate function that would return a specified number of rows picked up randomly without replacement from a data frame in R language? Can anyone help me out?

like image 345
nikhil Avatar asked Nov 25 '11 19:11

nikhil


People also ask

How do you remove a random row from a DataFrame in Python?

Solution. To remove rows at random without shuffling in Pandas DataFrame: Get an array of randomly selected row index labels. Use the drop(~) method to remove the rows.


1 Answers

First make some data:

> df = data.frame(matrix(rnorm(20), nrow=10)) > df            X1         X2 1   0.7091409 -1.4061361 2  -1.1334614 -0.1973846 3   2.3343391 -0.4385071 4  -0.9040278 -0.6593677 5   0.4180331 -1.2592415 6   0.7572246 -0.5463655 7  -0.8996483  0.4231117 8  -1.0356774 -0.1640883 9  -0.3983045  0.7157506 10 -0.9060305  2.3234110 

Then select some rows at random:

> df[sample(nrow(df), 3), ]            X1         X2 9  -0.3983045  0.7157506 2  -1.1334614 -0.1973846 10 -0.9060305  2.3234110 
like image 102
John Colby Avatar answered Sep 25 '22 17:09

John Colby