dplyr From Excel To R R

Move column to a specific position in R

Move column to a specific position in R

The easiest way to move the data frame column to a specific position in R is by using the function relocate from package dplyr.

It is common for me that after creating a new column, I want that to move to a specific location in the R data frame. There is a way to reorder data frame columns, but that is a lot of code for this simple task.

For example, here is the Iris dataset.

head(iris)
#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1          5.1         3.5          1.4         0.2  setosa
#2          4.9         3.0          1.4         0.2  setosa
#3          4.7         3.2          1.3         0.2  setosa
#4          4.6         3.1          1.5         0.2  setosa
#5          5.0         3.6          1.4         0.2  setosa
#6          5.4         3.9          1.7         0.4  setosa

I would like to move column Species to a first position. You can choose to specify a location in different ways – by using after or before arguments.

require(dplyr)

iris <- iris %>% relocate(Species, .before = Sepal.Length)

head(iris)
#  Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#1  setosa          5.1         3.5          1.4         0.2
#2  setosa          4.9         3.0          1.4         0.2
#3  setosa          4.7         3.2          1.3         0.2
#4  setosa          4.6         3.1          1.5         0.2
#5  setosa          5.0         3.6          1.4         0.2
#6  setosa          5.4         3.9          1.7         0.4

There is also possible to move a couple of columns to a specific position in R data frame.

iris <- iris %>% relocate(Petal.Width, Species, .after = Sepal.Length)

head(iris)
#  Sepal.Length Petal.Width Species Sepal.Width Petal.Length
#1          5.1         0.2  setosa         3.5          1.4
#2          4.9         0.2  setosa         3.0          1.4
#3          4.7         0.2  setosa         3.2          1.3
#4          4.6         0.2  setosa         3.1          1.5
#5          5.0         0.2  setosa         3.6          1.4
#6          5.4         0.4  setosa         3.9          1.7

Select range of columns in dplyr function

It is an advantage if you know how to select a range of columns in dplyr functions. If you know that you can use it also in relocate to move them to the necessary position.

head(iris %>% dplyr::select(Sepal.Width:Petal.Length))

#  Sepal.Width Petal.Length
#1         3.5          1.4
#2         3.0          1.4
#3         3.2          1.3
#4         3.1          1.5
#5         3.6          1.4
#6         3.9          1.7

Reorder range of columns in R

There is also possible to reorder a range of columns. You should only know how to select from one column to another in dplyr.

iris <- iris %>% relocate(Sepal.Width:Petal.Length, .before = Sepal.Length)

head(iris)
#  Sepal.Width Petal.Length Sepal.Length Petal.Width Species
#1         3.5          1.4          5.1         0.2  setosa
#2         3.0          1.4          4.9         0.2  setosa
#3         3.2          1.3          4.7         0.2  setosa
#4         3.1          1.5          4.6         0.2  setosa
#5         3.6          1.4          5.0         0.2  setosa
#6         3.9          1.7          5.4         0.4  setosa

 

Check out my favorite RStudio tips and tricks. For example, how to quickly view a data frame from R script.




0 comments on “Move column to a specific position in R

Leave a Reply

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