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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:21:10+00:00 2026-06-14T11:21:10+00:00

Possible Duplicate: faster way to create variable that aggregates a column by id I

  • 0

Possible Duplicate:
faster way to create variable that aggregates a column by id

I am having trouble with a project. I created a dataframe (called dat) in long format (i copied the first 3 rows below) and I want to calculate for example the mean of the Pretax Income of all Banks in the United States for the years 2000 to 2011. How would I do that? I have hardly any experience in R. I am sorry if the answer is too obvious, but I couldn’t find anything and i already spent a lot of time on the project. Thank you in advance!

KeyItem     Bank    Country Year    Value  
1   Pretax Income   WELLS_FARGO_&_COMPANY   UNITED STATES   2011    2.365600e+10                            
2   Total Assets    WELLS_FARGO_&_COMPANY   UNITED STATES   2011    1.313867e+12                            
3   Total Liabilities   WELLS_FARGO_&_COMPANY   UNITED STATES 2011  1.172180e+12                            
  • 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-14T11:21:12+00:00Added an answer on June 14, 2026 at 11:21 am

    The following should get you started. You basically need to do two things: subset, and aggregate. I’ll demonstrate a base R solution and a data.table solution.

    First, some sample data.

    set.seed(1) # So you can reproduce my results
    dat <- data.frame(KeyItem = rep(c("Pretax", "TotalAssets", "TotalLiabilities"), 
                                    times = 30),
                      Bank = rep(c("WellsFargo", "BankOfAmerica", "ICICI"), 
                                 each = 30),
                      Country = rep(c("UnitedStates", "India"), times = c(60, 30)),
                      Year = rep(c(2000:2009), each = 3, times = 3),
                      Value = runif(90, min=300, max=600))
    

    Let’s aggregate mean of the “Pretax” values by “Country” and “Year”, but only for the years 2001 to 2005.

    aggregate(Value ~ Country + Year, 
              dat[dat$KeyItem == "Pretax" & dat$Year >= 2001 & dat$Year <=2005, ], 
              mean)
    #         Country Year    Value
    # 1         India 2001 399.7184
    # 2  UnitedStates 2001 464.1638
    # 3         India 2002 443.5636
    # 4  UnitedStates 2002 560.8373
    # 5         India 2003 562.5964
    # 6  UnitedStates 2003 370.9591
    # 7         India 2004 404.0050
    # 8  UnitedStates 2004 520.4933
    # 9         India 2005 567.6595
    # 10 UnitedStates 2005 493.0583
    

    Here’s the same thing in data.table

    library(data.table)
    DT <- data.table(dat, key = "Country,Bank,Year")
    subset(DT, KeyItem == "Pretax")[Year %between% c(2001, 2005), 
        mean(Value), by = list(Country, Year)]
    #          Country Year       V1
    #  1:        India 2001 399.7184
    #  2:        India 2002 443.5636
    #  3:        India 2003 562.5964
    #  4:        India 2004 404.0050
    #  5:        India 2005 567.6595
    #  6: UnitedStates 2001 464.1638
    #  7: UnitedStates 2002 560.8373
    #  8: UnitedStates 2003 370.9591
    #  9: UnitedStates 2004 520.4933
    # 10: UnitedStates 2005 493.0583
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: faster way to create variable that aggregates a column by id So
Possible Duplicate: What loop is faster, while or for I see in three.js that
Possible Duplicate: Faster DirectoryExists function? I want to check if some file exists on
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc I
Possible Duplicate: Which is faster/best? SELECT * or SELECT column1, colum2, column3, etc. I
Possible Duplicate: Why is string concatenation faster than array join? Usually we need to
Possible Duplicate: Is it faster to count down than it is to count up?
Possible Duplicate: What is faster/prefered memset or for loop to zero out an array
Possible Duplicate: Is shifting bits faster than multiplying and dividing in Java? .NET? To
Possible Duplicate: Regex.IsMatch vs string.Contains Which is faster, preferable and why? What the difference

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.