Arranging rows in custom order using dplyr
We can use factor
to change the order
in a custom way
df %>%
arrange(factor(Reg, levels = LETTERS[c(3, 1, 2)]), desc(Res), desc(Pop))
# Reg Res Pop
#1 C Urban 501638
#2 C Rural 499274
#3 A Urban 500414
#4 A Rural 500501
#5 B Urban 499922
#6 B Rural 500016
Or with match
to get the index and arrange
on it
df %>%
arrange(match(Reg, c("C", "A", "B")), desc(Res), desc(Pop))
If we have multiple columns to arrange
in descending order
df %>%
arrange_at(2:3, desc) %>%
arrange(match(Reg, c("C", "A", "B")))
Arrange rows in custom order using R
You can make country
an ordered factor.
library(dplyr)
country <- rep(c("AT","BE","CY","DE","EE"),10)
value <- seq(1, 50)
# chenged this to a vector rather than a data frame
target_cc <- c("DE","CY","BE","AT","EE")
df %>%
mutate(country = factor(country, levels = target_cc)) %>%
arrange(country)
Reorder rows using custom order
First, create a vector with the letters in the desired order. Then match
* the vector with the variable to be sorted. match
returns indices of (first) matches, which can be plugged into slice
:
library(dplyr)
# create a vector with letters in the desired order
x <- c("C", "A", "B")
DT %>%
slice(match(x, category))
# category b
# 1 C 3
# 2 A 1
# 3 B 2
Another way would be to convert "category" to a factor
, set levels
to the desired order, and use arrange
:
DT %>%
mutate(category = factor(category, levels = x)) %>%
arrange(category)
# category b
# 1 C 3
# 2 A 1
# 3 B 2
*The match
method is inspired by this answer.
Arranging rows in R so that year column is in a custom order, and other columns with identical entries are grouped
Is this what you want?
library(dplyr)
USA_tech %>%
arrange(region, supplysector, subsector, technology, year)
#> region supplysector subsector technology year
#> 1 AK agriculture agriculture energy agriculture energy 1975
#> 2 AK agriculture agriculture energy agriculture energy 1990
#> 3 AK agriculture agriculture energy agriculture energy 2005
#> 4 AK agriculture agriculture energy agriculture energy 2010
#> 5 AK construction construction energy construction energy 1975
#> 6 AK construction construction energy construction energy 1990
#> 7 AK construction construction energy construction energy 2005
#> 8 AK construction construction energy construction energy 2010
#> 9 AK construction construction feedstocks construction feedstocks 1975
#> 10 AK construction construction feedstocks construction feedstocks 1990
#> 11 AK construction construction feedstocks construction feedstocks 2005
#> 12 AK construction construction feedstocks construction feedstocks 2010
#> 13 AL agriculture agriculture energy agriculture energy 1975
#> 14 AL agriculture agriculture energy agriculture energy 1990
#> 15 AL agriculture agriculture energy agriculture energy 2005
#> 16 AL agriculture agriculture energy agriculture energy 2010
#> 17 AL construction construction energy construction energy 1975
#> 18 AL construction construction energy construction energy 1990
#> 19 AL construction construction energy construction energy 2005
#> 20 AL construction construction energy construction energy 2010
#> 21 AL construction construction feedstocks construction feedstocks 1975
#> 22 AL construction construction feedstocks construction feedstocks 1990
#> 23 AL construction construction feedstocks construction feedstocks 2005
#> 24 AL construction construction feedstocks construction feedstocks 2010
#> coefficient market.name tech_change
#> 1 0.004152838 AK NA
#> 2 0.004285719 AK NA
#> 3 0.005530093 AK NA
#> 4 0.005155489 AK NA
#> 5 0.002515274 AK NA
#> 6 0.005207937 AK NA
#> 7 0.007962923 AK NA
#> 8 0.006292851 AK NA
#> 9 0.005392370 AK NA
#> 10 0.003550397 AK NA
#> 11 0.002824053 AK NA
#> 12 0.001722293 AK NA
#> 13 0.010508707 AL NA
#> 14 0.006022474 AL NA
#> 15 0.007286581 AL NA
#> 16 0.005622613 AL NA
#> 17 0.004010048 AL NA
#> 18 0.002468304 AL NA
#> 19 0.002889554 AL NA
#> 20 0.002351508 AL NA
#> 21 0.001856025 AL NA
#> 22 0.002650908 AL NA
#> 23 0.004949693 AL NA
#> 24 0.003576161 AL NA
Created on 2020-09-24 by the reprex package (v0.3.0)
When using arrange
, the data gets ordered in the order you specify the columns.
Sort dataframe by custom order
If you want to keep the column as character rather than factor, you can arrange based on a match
to the order vector
rating_order <- c("AAA", "AA", "A", "BBB", "BB", "B")
df <-
data.frame(
Rating = c("A", "AA", "AAA", "B", "BB", "BBB"),
Value1 = c(1, 2, 3, 4, 5, 6),
Value2 = c(2, 3, 4, 5, 3, 2)
)
library(dplyr, warn.conflicts = FALSE)
df %>%
arrange(match(Rating, rating_order))
#> Rating Value1 Value2
#> 1 AAA 3 4
#> 2 AA 2 3
#> 3 A 1 2
#> 4 BBB 6 2
#> 5 BB 5 3
#> 6 B 4 5
Created on 2022-01-20 by the reprex package (v2.0.1)
Arrange by custom order in dplyr using dynamic column names
You can also use as.symbol
from base R
test_tbl_final <- test_tbl %>%
dplyr::arrange(factor(!!as.symbol(group_var), levels = order_var), date)
Related Topics
Compute Projection/Hat Matrix via Qr Factorization, Svd (And Cholesky Factorization)
How to Prep Transaction Data into Basket for Arules
Manipulating Files with Non-English Names in R
Differencebetween Short (&,|) and Long (&&, ||) Forms of And, or Logical Operators in R
Blend of Na.Omit and Na.Pass Using Aggregate
Plot a Character Vector Against a Numeric Vector in R
How to Split Data Frame by Column Names in R
Can Sparklyr Be Used with Spark Deployed on Yarn-Managed Hadoop Cluster
Harvest (Rvest) Multiple HTML Pages from a List of Urls
How to Optimize for Integer Parameters (And Other Discontinuous Parameter Space) in R
How to Change Color in Shiny Dashboard
How to Specify Names of Columns for X and Y When Joining in Dplyr