How to shuffle a dataframe in R by rows
In this article, we will discuss how to shuffle a dataframe by rows in the R programming language.
Shuffling means reordering or rearranging the data. We can shuffle the rows in the dataframe by using sample() function. By providing indexing to the dataframe the required task can be easily achieved.
- dataframe is the input dataframe
- sample() function is used to shuffle the rows that takes a parameter with a function called nrow() with a slice operator to get all rows shuffled.
- nrow() is sued to get all rows by taking the input parameter as a dataframe
Example: R program to create a dataframe with 3 columns and 6 rows and shuffle the dataframe by rows
Example: R program to create a student dataframe with 2 columns and 3 rows