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 8044051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:06:18+00:00 2026-06-05T05:06:18+00:00

Say I had the dataframe df <- data.frame(‘A’ = c(‘a’,’a’,’a’,’a’,’b’,’b’,’b’,’b’,’b’), ‘B’ = c(‘y’,’y’,’z’,’z’,’y’,’y’,’y’,’z’,’z’), ‘value’=c(1

  • 0

Say I had the dataframe

df <- data.frame('A' = c('a','a','a','a','b','b','b','b','b'),
                 'B' = c('y','y','z','z','y','y','y','z','z'),
                 'value'=c(1  , 2 , 2 , 3 , 2 , 3 , 1 , 2 , 2))

so it looked like this

 A B value  
 a y     1  
 a y     2  
 a z     2  
 a z     3  
 b y     2  
 b y     3  
 b y     1   
 b z     2   
 b z     2  

I could get the mean of each subset of A and B using the query

with(df, aggregate(df, by = list(A, B), FUN = mean))

which after a bit of manipulation gives

A B value  
a y   1.5  
b y   2.0  
a z   2.5  
b z   2.0  

Is there are way of doing this but only calculating the mean of the highest x values in each subset. So if we take x as 2 in this example the mean of the subsets ay, az, and bz would not change as they only have a total of two entries (so the top x entries are the entire dataset of the subset). However by has three entries so we would want to return the mean of the highest two values (2 and 3) so that the output table would look like

A B value  
a y   1.5  
b y   2.5  
a z   2.5  
b z   2.0  
  • 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-05T05:06:20+00:00Added an answer on June 5, 2026 at 5:06 am

    I find it easier to use the formula interface to aggregate, as follows:

    Your original version:

    aggregate(value~A+B, data=df, FUN = mean)
      A B value
    1 a y   1.5
    2 b y   2.0
    3 a z   2.5
    4 b z   2.0
    

    You can get your desired version by using an anonymous function that computes the mean of the tail of the sorted values:

    aggregate(value~A+B, data=df, FUN = function(x)mean(tail(sort(x), 2)))
      A B value
    1 a y   1.5
    2 b y   2.5
    3 a z   2.5
    4 b z   2.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking for a non-JS solution. Say I had something like this: <ul> <li> <form>
So say i had some text like this: line num 1 line num 2
Lets say you had some resource clean up like: This is C#. try{/*stuff*/} catch(Exception
Let's say I had a class like this: class Parser attr_accessor :config, :html def
So...say I had a function like this... int function( const char *c ) {
say I had a stored proc like this: BEGIN DECLARE user_id INT; SET user_id
Let's say I had an xml file like this: <items> <item> <id>1</id> <name>Item 1</name>
Say I had a function such as this one procedure TMyObject.DoSomething(text: string); begin //
Say I had a fact such as: bigger(cat,mouse). If I wanted to describe this
Say I had some data, for which I want to fit a parametrized model

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.