Sample random rows in DataFrame in R

0 votes
I am having a problem in finding a proper function that would return specific number of rows picked randomly without replacement from  a data frame?
Apr 13, 2018 in Data Analytics by Dynamic_Coder22
• 140 points
206 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

First we should have some data:

df1<-data.frame(matrix(rnorm(20), nrow=10))
> df1
           X1          X2
1  -0.5996083  0.80017687
2  -0.1294107 -0.16393097
3   0.8867361  1.24291877
4  -0.1513960 -0.93438506
5   0.3297912  0.39370865
6  -3.2273228  0.40363146
7  -0.7717918 -0.88643672
8   0.2865486 -1.31893760
9  -1.2205120  0.02884391
10  0.4345504 -0.43212979

Then select some rows at random:

df1[sample(nrow(df1), 3), ]
          X1         X2
10 0.4345504 -0.4321298
5  0.3297912  0.3937087
8  0.2865486 -1.3189376
answered Apr 13, 2018 by DeepCoder786
• 1,700 points

Related Questions In Data Analytics

0 votes
1 answer

How to sample random rows in dataframe?

Create data frame and then implement as ...READ MORE

answered Jul 2, 2018 in Data Analytics by darklord
• 6,140 points
19 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,140 points
51 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
29 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,700 points
62 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,700 points
32 views
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,700 points
23 views
0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
54 views
0 votes
1 answer

How to save a DataFrame in R??

Suppose your DataFrame is named as df: write.csv(df,file="exmp.csv") Then ...READ MORE

answered Apr 20, 2018 in Data Analytics by DeepCoder786
• 1,700 points
167 views
0 votes
7 answers

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.