Code Examples of How to Reorder columns in R

When doing data science, it is sometimes necessary when working with a data frame to reorder columns in r. There are several functions that you can use to do a reordering of columns. It is a straightforward process that allows you to rearrange the columns in any order that you want.

Description

When you want to reorder multiple columns, r programming offers more than one order function that allows you to rearrange the column position of a specific column. The basic order function has the format of df[ , c(column1, column2, column3)] where df is the data frame and column1, column2, and column3 are column names or original numerical order. There can be two or more column names. There is also the subset function which has the format of subset(df, select = c(column1, column2, column3)) With the same meanings as used earlier. You can use these functions to rearrange the columns any way that you want to.

Explanation

When you reorder the columns of a data frame, you are essentially reorganizing a variable list. This process only affects the column name order and the corresponding order of values within each row. The row names and order are not affected by this process. What you are doing is rearranging the columns so that they are in the order that you prefer. The column order can be any possible arrangement of the columns with which you are working. However, some arrangements make more sense than others, and putting columns in a more meaningful order is one of the reasons why you would reorder columns

Examples of How to Reorder columns in R

Here are three examples of how to reorder columns in r. The first two are variations of the same function while the second uses a different function.

> df = data.frame(Cat = 1:7,
+ Bat = LETTERS[1:7],
+ Mat = c(2,4,6,8,10,12,14))
> df
Cat Bat Mat
1 1 A 2
2 2 B 4
3 3 C 6
4 4 D 8
5 5 E 10
6 6 F 12
7 7 G 14
> df[ , c(2, 1, 3)]
Bat Cat Mat
1 A 1 2
2 B 2 4
3 C 3 6
4 D 4 8
5 E 5 10
6 F 6 12
7 G 7 14

Here we use the basic order function using numerical column order to swap the first column and the second column.

> df = data.frame(Cat = 1:7,
+ Bat = LETTERS[1:7],
+ Mat = c(2,4,6,8,10,12,14))
> df
Cat Bat Mat
1 1 A 2
2 2 B 4
3 3 C 6
4 4 D 8
5 5 E 10
6 6 F 12
7 7 G 14
> df[ , c(“Bat”, “Cat”, “Mat”)]
Bat Cat Mat
1 A 1 2
2 B 2 4
3 C 3 6
4 D 4 8
5 E 5 10
6 F 6 12
7 G 7 14

Here we use the basic order function using the column names to swap the first column and the second column.

> df = data.frame(Cat = 1:7,
+ Bat = LETTERS[1:7],
+ Mat = c(2,4,6,8,10,12,14))
> df
Cat Bat Mat
1 1 A 2
2 2 B 4
3 3 C 6
4 4 D 8
5 5 E 10
6 6 F 12
7 7 G 14
> subset(df, select = c(2, 1, 3))
Bat Cat Mat
1 A 1 2
2 B 2 4
3 C 3 6
4 D 4 8
5 E 5 10
6 F 6 12
7 G 7 14

Here we use the subset function using numerical column order to swap the first column and the second column. All three of these methods are effective and the one that you use will depend on the situation including the information that you have and the size of the data frame.

Application of How to Reorder columns in R

There are several applications for reordering the columns in a data frame. You can place the columns in alphabetical order or in ascending order numerically. This process can be used to place the columns in a more meaningful order. It can even be used to completely scramble the column order, although why you would do such is questionable. The most common applications are reordering the columns alphabetically and numerically, but other arrangements can be more meaningful under the right circumstances. Such a situation can occur when you have two closely related columns, having them next to each other could reveal patterns that would not be seen otherwise.

When you are reordering columns, R programming supplies more than one way of doing so. This increases the flexibility beyond what you would get with a simple function. The fact that you can use either column names or current column numerical orders supplies even more flexibility. It is a useful tool with several different options.

Scroll to top
Privacy Policy