How to sample random rows in dataframe?

0 votes

I want to return a specified number of rows picked up randomly without replacement from a data frame in R language? Can anyone help me out?

Thanks!

Jul 2, 2018 in Data Analytics by anonymous
61 views

1 answer to this question.

0 votes

Create data frame and then implement as follows:

> 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
answered Jul 2, 2018 by darklord
• 6,190 points

Related Questions In Data Analytics

0 votes
1 answer

Sample random rows in DataFrame in R

First we should have some data: df1<-data.frame(matrix(rnorm(20), nrow=10)) > ...READ MORE

answered Apr 13, 2018 in Data Analytics by DeepCoder786
• 1,720 points
733 views
0 votes
7 answers
0 votes
1 answer

How to drop rows of Pandas DataFrame whose value in certain coulmns is NaN

Easy Way: This Way we can easily handle ...READ MORE

answered May 7, 2018 in Data Analytics by DeepCoder786
• 1,720 points
687 views
0 votes
2 answers
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
137 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 45,870 points
69 views
0 votes
1 answer
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
179 views
0 votes
1 answer

How to sample n random rows per group in a dataframe?

You can assign a random ID to ...READ MORE

answered Jul 2, 2018 in Data Analytics by darklord
• 6,190 points
466 views
0 votes
2 answers

How to remove rows with missing values (NAs) in a data frame?

Hi, The below code returns rows without ...READ MORE

answered Aug 20 in Data Analytics by anonymous
• 32,260 points
6,067 views