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
347 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,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
33 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
143 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 in Data Analytics by anonymous
• 19,510 points
19 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
57 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,700 points
94 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
53 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
66 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
86 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
335 views
0 votes
7 answers