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
• 180 points
7,835 views

1 answer to this question.

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,720 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 3, 2018 in Data Analytics by Sahiti
• 6,370 points
588 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 3, 2018 in Data Analytics by Sahiti
• 6,370 points
4,682 views
0 votes
1 answer

fetch all rows with null values in dataframe or dataset - R

Use below code -  which(is.na(air)) is.na() will return a boolean ...READ MORE

answered Jul 4, 2019 in Data Analytics by anonymous
• 33,030 points
10,633 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,660 points
814 views
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
1,605 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,720 points
997 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,720 points
1,441 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,417 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,720 points
4,801 views
0 votes
7 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP