Randomly Reorder Data Frame by Row and Column in R (2 Examples)

 

In this R tutorial you’ll learn how to shuffle the rows and columns of a data frame randomly.

The article contains two examples for the random reordering. More precisely, the content of the post is structured as follows:

Let’s get started…

 

Creation of Example Data

We’ll use the data below as basement for this R programming tutorial:

data <- data.frame(x1 = 1:10,                # Create example data
                   x2 = LETTERS[1:10])
data                                         # Print example data
#    x1 x2
# 1   1  A
# 2   2  B
# 3   3  C
# 4   4  D
# 5   5  E
# 6   6  F
# 7   7  G
# 8   8  H
# 9   9  I
# 10 10  J

Have a look at the previous output of the RStudio console. It shows that our exemplifying data has ten rows and two columns. The variable x1 contains numeric values ranging from 1 to 10 and the variable x2 contains alphabetic letters ranging from A to J.

Let’s shuffle these data!

 

Example 1: Shuffle Data Frame by Row

In Example 1, I’ll show how to reorder a data matrix rowwise. First, we need to set a seed for reproducibility:

set.seed(2347723)                            # Set seed

Now, we can use the sample and nrow functions as shown below:

data_row <- data[sample(1:nrow(data)), ]     # Randomly reorder rows
data_row                                     # Print updated data
#    x1 x2
# 4   4  D
# 7   7  G
# 5   5  E
# 1   1  A
# 10 10  J
# 6   6  F
# 8   8  H
# 3   3  C
# 9   9  I
# 2   2  B

As you can see based on the previous output of the RStudio console, our updated data frame is permuted rowwise.

 

Example 2: Shuffle Data Frame by Column

In Example 2, I’ll show how to permute a data frame columnwise. Again, we need to set a seed for reproducibility first:

set.seed(8732645)                            # Set seed

Now, we can use a combination of the sample and ncol functions to shuffle our data by variables:

data_col <- data[ , sample(1:ncol(data))]    # Randomly reorder columns
data_col                                     # Print updated data
#    x2 x1
# 1   A  1
# 2   B  2
# 3   C  3
# 4   D  4
# 5   E  5
# 6   F  6
# 7   G  7
# 8   H  8
# 9   I  9
# 10  J 10

Have a look at the previous output: The two variables x1 and x2 were exchanged.

 

Video & Further Resources

Do you need further info on the R programming syntax of this post? Then you may want to watch the following video of my YouTube channel. In the video, I illustrate the examples of this post in the R programming language.

 

 

Furthermore, you might have a look at the related tutorials of this website. Some articles can be found below.

 

In this R tutorial you learned how to permute or randomize the row and column ordering of a data frame. In case you have any additional questions, let me know in the comments section.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top