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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:43:57+00:00 2026-06-16T15:43:57+00:00

Probably, I haven’t defined the problem very well. I don’t seem to understand what

  • 0

Probably, I haven’t defined the problem very well. I don’t seem to understand what R is returning out of sapply. I have a large dataframe of hierarchical data. About half the columns are factors and half are numerical. I want to get a new dataframe that contains some of the factors, and sums over the numerical columns, but I want the sums to remain separated by factor levels.

For instance, from the sample data below, I’d like to make a dataframe with the state, district, branch the same, but sum the data for orders of the same type but with different colours. I’m thinking that iterative use of sapply will do it, but I can’t seem to get it to work.

sample data:

state district branch   order   colour  number  cost    amount
CA   central newtown    shoes   black   6   25.50  127.40
CA   central newtown    shoes   brown   3   32.12   75.40
CA   central newtown    gloves  blue    15  12.20  157.42
CA   central newtown    gloves  black   9   8.70    65.37
CA  central columbus    shoes   black   12  30.75   316.99
CA  central columbus    shoes   brown   1   40.98    45.00
CA  central columbus    gloves  blue    47  11.78   498.32
CA  central columbus    gloves  black   23  7.60    135.50
  • 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-16T15:43:58+00:00Added an answer on June 16, 2026 at 3:43 pm

    Another job for aggregate. Calling your data frame dat:

    aggregate(cbind(cost, amount) ~ state+district+branch+order, data=dat, FUN=sum)
    
    ##   state district   branch  order  cost amount
    ## 1    CA  central columbus gloves 19.38 633.82
    ## 2    CA  central  newtown gloves 20.90 222.79
    ## 3    CA  central columbus  shoes 71.73 361.99
    ## 4    CA  central  newtown  shoes 57.62 202.80
    

    On the left side of the ~, cbind is used to indicate that we want each column separately. If cost + amount were specified, it would mean the sum here because these are numeric. On the right side of the ~, we have factors, so the + means that we are aggregating by each level of each factor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

well i have most probably an extremly stupid problem but could not figure it
This i probably trival, but I haven't figured it out yet. I have a
This is probably a very easy question to answer, but I haven't figure out
This is another probably very simple question, but I haven't been able to find
This is probably pretty simple but I haven't been able to figure out how
This is probably a very basic question but I haven't found any detailed information
I haven't seen any documentation on them, so they probably don't exist, but it's
This is probably a very trivial question, but I haven't been able to find
I probably haven't worded the title of this question particularly well in the slightest.
This is probably a silly question, but I have looked and haven't found a

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.