How to use select() method In R Language
We are going to use a select() method to reorder columns.
Syntax: select(dataframe,columns)
where
- dataframe is the input dataframe
- columns are the input columns to be reordered
Here we are rearranging the dataframe(id,department,salary) to (salary,id,department)
R
print ( "Before: " ) data print ( "After: " ) # reorder the columns using select select (data, salary, id, department) |
Output:
Reorder the column of dataframe in R using Dplyr
In this article, we will discuss how to rearrange or reorder the column of the dataframe using dplyr package in R Programming Language.
Creating Dataframe for demonstration:
R
# load the package library (dplyr) # create the dataframe with three columns # id , department and salary with 8 rows data = data.frame (id = c (7058, 7059, 7060, 7089, 7072, 7078, 7093, 7034), department = c ( 'IT' , 'sales' , 'finance' , 'IT' , 'finance' , 'sales' , 'HR' , 'HR' ), salary = c (34500.00, 560890.78, 67000.78, 25000.00, 78900.00, 25000.00, 45000.00, 90000)) # display dataframe data |
Output: