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

  • SEARCH
  • Home
  • 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 6228087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:19:47+00:00 2026-05-24T09:19:47+00:00

For example, I have a data frame with data across categories and subcategories and

  • 0

For example, I have a data frame with data across categories and subcategories and I want to be able to get row with maximum value in a particular column etc.

SQL is what comes to mind first. But since I am not interested in joins or indices etc, python’s list comprehensions would do the same thing better with a more modern syntax.

What’s best practice in R for such operations?

EDIT:
For now I think I am fine with which.max. Why I asked the question the way I did is simply that I have come to learn that in R there are many libraries etc doing pretty much the same thing. Just by reading the documentation it’s very hard to evaluate how popular (ie how well the library fulfills its purpose). My personal experience with Python is that the day you figure out how to use list comprehensions (with itertools as a bonus), you are pretty much covered. Over time this has evolved as best practice, you don’t see lambda and filter for example that often in the general python debate these days as list comprehensions does the same thing easier and more uniform.

  • 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-05-24T09:19:47+00:00Added an answer on May 24, 2026 at 9:19 am

    Some additional context would help, but from the sounds of it – you may be looking for which.max() or the related functions. For group by operations, I default to the plyr family of functions, but there are certainly faster alternatives in base R if speed is of utmost importance.

    library(plyr)
    #Make a local copy of mycars data and add the rownames as a column since ddply
    #seems to drop them. I've never encountered that before actually...
    myCars <- mtcars
    myCars$carname <- rownames(myCars)
    
    #Find the max mpg
    myCars[which.max(myCars$mpg) ,]
    
                    mpg cyl disp hp drat    wt qsec vs am gear carb        carname
    Toyota Corolla 33.9   4 71.1 65 4.22 1.835 19.9  1  1    4    1 Toyota Corolla
    
    #Find the max mpg by cylinder category
    ddply(myCars, "cyl", function(x) x[which.max(x$mpg) ,])
    
       mpg cyl  disp  hp drat    wt  qsec vs am gear carb          carname
    1 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1   Toyota Corolla
    2 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1   Hornet 4 Drive
    3 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2 Pontiac Firebird
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large data frame with the following fields (example data). #dput(data) gives...
I have a data.frame in R which has been constructed off the Example 1-3
i have a data frame like this A B value 1 1 0.123 2
Suppose I have a data.frame with N rows. The id column has 10 unique
I have a data frame with a character column of date-times. When I use
Possible Duplicate: Extracting indices for data frame rows that have MAX value for named
I often have data where I want to compare the value of one level
If I have a data.frame containing the data I want to plot, what is
I have a data frame of two columns: key and value and I would
I know if I have a data frame with more than 1 column, then

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.