How to reshuffle columns in R?

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.

Syntax:

 dataframe[sample(1:nrow(dataframe)), ]
Where.

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:

# create a dataframe of students with id,name and marks
data=data.frame(id=c(1,2,3,4,5,6),
                name=c("sravan","bobby","ojaswi","gnanesh",
                       "rohith","satwik"),
                marks=c(89,90,98,78,98,78))
  
# display dataframe
print(data)
print("_______________________________________________________")
  
# shuffle the dataframe by rows
shuffled_data= data[sample(1:nrow(data)), ]
  
# display
print(shuffled_data)

Output:

 id name   marks 
1 1 sravan  89 
2 2 bobby   90 
3 3 ojaswi  98 
4 4 gnanesh 78
5 5 rohith  98
6 6 Satwik  78
[1] "_
 
 id name    marks 
4 4 gnanesh 78 
5 5 rohith  98 
2 2 bobby   90 
3 3 ojaswi  98
6 6 satwik  78 
1 1 sravan  89