- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to change the order of one column data frame and get the output in data frame format in R?
To change the order of one column data frame and get the output in data frame format in R, we can follow the below steps −
- First of all, create a data frame.
- Then, use order function to change the order of column with drop argument set to FALSE
Create the data frame
Let's create a data frame as shown below −
> x<-rnorm(20) > df<-data.frame(x) > df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x 1 -0.13734270 2 -1.02796577 3 1.40171778 4 -0.45367796 5 0.06634050 6 -1.27974403 7 -0.37548120 8 1.14533286 9 0.63468234 10 -0.25081200 11 -1.33503444 12 1.61475941 13 -0.23285412 14 0.47466024 15 0.85957117 16 0.61110128 17 -1.35330301 18 -0.73807621 19 0.10654000 20 0.07606264
Change the order of column
Example
Using order function with drop argument to change the order of column and return the output in data frame format −
> x<-rnorm(20) > dflt;-data.frame(x) > df[order(df$x),,drop=FALSE]
Output
x 17 -1.35330301 11 -1.33503444 6 -1.27974403 2 -1.02796577 18 -0.73807621 4 -0.45367796 7 -0.37548120 10 -0.25081200 13 -0.23285412 1 -0.13734270 5 0.06634050 20 0.07606264 19 0.10654000 14 0.47466024 16 0.61110128 9 0.63468234 15 0.85957117 8 1.14533286 3 1.40171778 12 1.61475941
- Related Articles
- How to change the row order in an R data frame?
- How to change the order of columns in an R data frame?
- How to sort one column of an R data frame in ascending and the other in descending order?
- How to change the column names and row names of a data frame in R?
- How to change the code "Yes" to 1 in an R data frame column?
- How to change the name of a data frame in R?
- How to get top values of a numerical column of an R data frame in decreasing order?
- How to change the sign of even number rows in an R data frame column?
- How to match a column in a data frame with a column in another data frame in R?
- Change the decimal point of every value in an R data frame column.
- How to change a column in an R data frame with some conditions?
- How to extract a single column of an R data frame as a data frame?
- How to select only one column from an R data frame and return it as a data frame instead of vector?
- How to display the data frame summary in vertical order in R?
- How to add one column in an R data frame to rest of the next columns?

Advertisements