- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to randomly select a fixed number of rows without replacement from a data frame in R?
This can be done simply by using sample function.
Example
> df = data.frame(matrix(rnorm(20), nrow=5)) > df X1 X2 X3 X4 1 -0.3277833 -0.1810403 0.2844406 -2.9676440 2 0.8262923 0.4334449 0.4031084 -1.9278049 3 -0.1769219 -0.1583660 -0.2829540 -0.1962654 4 1.0357773 0.9326049 0.3250011 -1.8835882 5 -1.0682642 -0.6589731 -0.4783144 -0.2945062
Let’s say we want to select 3 rows randomly then it can be done as follows −
> df[sample(nrow(df), 3), ] X1 X2 X3 X4 2 0.8262923 0.4334449 0.4031084 -1.9278049 1 -0.3277833 -0.1810403 0.2844406 -2.9676440 5 -1.0682642 -0.6589731 -0.4783144 -0.2945062
Advertisements