Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Sort in R

Tags:

r

dplyr

I have the following data frame in R.

# Create a data frame with duplicates
df <- data.frame(
  pagePath = c(
    "/page7",
    "/page9",
    "/page3",
    "/page9",
    "/page5",
    "/page7",
    "/page4",
    "/page9",
    "/page9",
    "/page4",
    "/page5",
    "/page7"
  ),
  country = c(
    "USA",
    "Canada",
    "UK",
    "Germany",
    "France",
    "India",
    "Australia",
    "Japan",
    "Brazil",
    "India",
    "South Africa",
    "Canada"
  )
)

I want to sort data based on ordering of unique pagePath.. For example, first all rows of "/page7" should appear, then followed by all rows of "/page9", then "/page3" etc.

like image 565
Ujjawal Bhandari Avatar asked Aug 02 '26 14:08

Ujjawal Bhandari


1 Answers

You may use match and unique -

library(dplyr)

df %>% arrange(match(pagePath, unique(pagePath)))

#   pagePath      country
#1    /page7          USA
#2    /page7        India
#3    /page7       Canada
#4    /page9       Canada
#5    /page9      Germany
#6    /page9        Japan
#7    /page9       Brazil
#8    /page3           UK
#9    /page5       France
#10   /page5 South Africa
#11   /page4    Australia
#12   /page4        India

Or use factor -

df %>% arrange(factor(pagePath, unique(pagePath)))

This can also be done in base R -

df[with(df, order(match(pagePath, unique(pagePath)))), ]
like image 52
Ronak Shah Avatar answered Aug 04 '26 04:08

Ronak Shah