Reordering Data Frame Columns in R
Previously, we described the essentials of R programming and provided quick start guides for importing data into R as well as converting your data into a tibble data format, which is the best and modern way to work with your data. We also described crutial steps to reshape your data with R for easier analyses.
Pleleminary tasks
Launch RStudio as described here: Running RStudio and setting up your working directory
Prepare your data as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files
Import your data into R as described here: Fast reading of data from txt|csv files into R: readr package.
Here, we’ll use the R built-in iris data set, which we start by converting to a tibble data frame (tbl_df). Tibble is a modern rethinking of data frame providing a nicer printing method. This is useful when working with large data sets.
# Create my_data
my_data <- iris
# Convert to a tibble
library("tibble")
my_data <- as_data_frame(my_data)
# Print
my_data
Source: local data frame [150 x 5]
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
7 4.6 3.4 1.4 0.3 setosa
8 5.0 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa
10 4.9 3.1 1.5 0.1 setosa
.. ... ... ... ... ...
Reorder column by position
# Get column names
colnames(my_data)
[1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
my_data contains 5 columns ordered as follow:
- Sepal.Length
- Sepal.Width
- Petal.Length
- Petal.Width
- Species
But we want:
- the variable “Species” to be the first column (1)
- the variable “Petal.Width” to be the second column (2)
It’s possible to reorder the column by position as follow:
my_data2 <- my_data[, c(5, 4, 1, 2, 3)]
my_data2
Source: local data frame [150 x 5]
Species Petal.Width Sepal.Length Sepal.Width Petal.Length
1 setosa 0.2 5.1 3.5 1.4
2 setosa 0.2 4.9 3.0 1.4
3 setosa 0.2 4.7 3.2 1.3
4 setosa 0.2 4.6 3.1 1.5
5 setosa 0.2 5.0 3.6 1.4
6 setosa 0.4 5.4 3.9 1.7
7 setosa 0.3 4.6 3.4 1.4
8 setosa 0.2 5.0 3.4 1.5
9 setosa 0.2 4.4 2.9 1.4
10 setosa 0.1 4.9 3.1 1.5
.. ... ... ... ... ...
Reorder column by name
col_order <- c("Species", "Petal.Width", "Sepal.Length",
"Sepal.Width", "Petal.Length")
my_data2 <- my_data[, col_order]
my_data2
Source: local data frame [150 x 5]
Species Petal.Width Sepal.Length Sepal.Width Petal.Length
1 setosa 0.2 5.1 3.5 1.4
2 setosa 0.2 4.9 3.0 1.4
3 setosa 0.2 4.7 3.2 1.3
4 setosa 0.2 4.6 3.1 1.5
5 setosa 0.2 5.0 3.6 1.4
6 setosa 0.4 5.4 3.9 1.7
7 setosa 0.3 4.6 3.4 1.4
8 setosa 0.2 5.0 3.4 1.5
9 setosa 0.2 4.4 2.9 1.4
10 setosa 0.1 4.9 3.1 1.5
.. ... ... ... ... ...
Summary
References
Infos
This analysis has been performed using R (ver. 3.2.3).
Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!
Recommended for You!
Recommended for you
This section contains the best data science and self-development resources to help you on your path.
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Click to follow us on Facebook :
Comment this article by clicking on "Discussion" button (top-right position of this page)