Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:26:03+00:00 2026-06-01T06:26:03+00:00

I have a data frame with a grouping variable ‘ID’ and some values (‘Value’):

  • 0

I have a data frame with a grouping variable ‘ID’ and some values (‘Value’):

dt <- data.frame(
        ID = c('A1','A2','A4','A2','A1','A4','A3','A2','A1','A3'),
        Value = c(4,3,1,3,4,6,6,1,8,4)
    )
dt
#    ID Value
# 1  A1     4
# 2  A2     3
# 3  A4     1
# 4  A2     3
# 5  A1     4
# 6  A4     6
# 7  A3     6
# 8  A2     1
# 9  A1     8
# 10 A3     4

I can calculate an overall rank order of the ‘Value’ column like this:

dt$Order <- rank(dt$Value, ties.method = "first")
dt
#    ID Value Order
# 1  A1     4     5
# 2  A2     3     3
# 3  A4     1     1
# 4  A2     3     4
# 5  A1     4     6
# 6  A4     6     8
# 7  A3     6     9
# 8  A2     1     2
# 9  A1     8    10
# 10 A3     4     7

But how can I calculate rank order within each ‘ID’, instead of a global rank order?

#    ID Value  rnk
# 1  A1     4    1
# 2  A2     3    2
# 3  A4     1    1
# 4  A2     3    3
# 5  A1     4    2
# 6  A4     6    2
# 7  A3     6    2
# 8  A2     1    1
# 9  A1     8    3
# 10 A3     4    1

In T-SQL, we can get this done as the following syntax:

RANK() OVER ( [ < partition_by_clause > ] < order_by_clause > )

Any idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:26:05+00:00Added an answer on June 1, 2026 at 6:26 am

    My way but there’s likely better. Never used rank, din’t even know about it. Thanks, may be useful.

    #Your Data
    dt <- data.frame(
        ID = c('A1','A2','A4','A2','A1','A4','A3','A2','A1','A3'),
        Value = c(4,3,1,3,4,6,6,1,8,4)
    )
    dt$Order <- rank(dt$Value,ties.method= "first")
    
    #My approach
    dt$id <- 1:nrow(dt) #needed for ordering and putting things back together
    dt <- dt[order(dt$ID),]
    dt$Order.by.group <- unlist(with(dt, tapply(Value, ID, function(x) rank(x, 
        ties.method = "first"))))
    dt[order(dt$id), -4]
    

    Yields:

       ID Value Order Order.by.group
    1  A1     4     5              1
    2  A2     3     3              2
    3  A4     1     1              1
    4  A2     3     4              3
    5  A1     4     6              2
    6  A4     6     8              2
    7  A3     6     9              2
    8  A2     1     2              1
    9  A1     8    10              3
    10 A3     4     7              1
    

    EDIT:

    If you don’t care about preserving the original order of the data then this works with less code:

    dt <- dt[order(dt$ID),]
    dt$Order.by.group <- unlist(with(dt, tapply(Value, ID, function(x) rank(x, 
       ties.method= "first"))))
    
       ID Value Order.by.group
    1  A1     4              1
    5  A1     4              2
    9  A1     8              3
    2  A2     3              2
    4  A2     3              3
    8  A2     1              1
    7  A3     6              2
    10 A3     4              1
    3  A4     1              1
    6  A4     6              2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame with two columns: an ID and some numeric value
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame which can be generated with: a <- c(rep(w,3), rep(x,3),
I have a data frame and I would like to impute missing values based
i have a data frame like this A B value 1 1 0.123 2
I have a data frame, and I want to do some calculation with existing
I have a data.frame in panel format (country-year) and I need to calculate the
I have a data.frame of cells, values and coordinates. It resides in the global
I have a data frame with an year of daily values of rainfall (complete
I have a data.frame with column headers. How can I get a specific row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.