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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:16:33+00:00 2026-06-17T16:16:33+00:00

Say I had the following table DataTable Cat1 | Cat2 | Val1 | Val2

  • 0

Say I had the following table DataTable

Cat1    |   Cat2    |   Val1    |   Val2
--------------------------------------------
A       |   A       |   1       |   2
A       |   B       |   3       |   4
B       |   A       |   5       |   6
B       |   B       |   7       |   8
A       |   A       |   2       |   4
A       |   B       |   6       |   8
B       |   A       |   10      |   12
B       |   B       |   14      |   16

Which I wanted to Aggregate by Cat1 and Cat2, taking the Sum and Avg of Val1 and Val2 respectively, how might I acheive this?

Cat1    |   Cat2    | Sum Val1  | Avg Val2
--------------------------------------------
A       |   A       |   3       |   3
A       |   B       |   9       |   6
B       |   A       |   15      |   9
B       |   B       |   21      |   12

I’ve achieved single variable aggregation with the aggregate function:

aggregate(
        Val1
    ~   Cat1 + Cat2
    data=DataTable,
    FUNC=sum
)

but despite playing around with cbind, can’t get the behaviour I want. I’m 24 hrs into learning R, so I’m not familiar enough with the concepts to fully understand what I’ve been doing (always dangerous!) but think this must be simple to achieve.
|

  • 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-17T16:16:35+00:00Added an answer on June 17, 2026 at 4:16 pm
    set.seed(45)
    df <- data.frame(c1=rep(c("A","A","B","B"), 2), 
                     c2 = rep(c("A","B"), 4), 
                     v1 = sample(8), 
                     v2 = sample(1:100, 8))
    > df
    #   c1 c2 v1 v2
    # 1  A  A  6 19
    # 2  A  B  3  1
    # 3  B  A  2 37
    # 4  B  B  8 86
    # 5  A  A  5 30
    # 6  A  B  1 44
    # 7  B  A  7 41
    # 8  B  B  4 39
    
    v1 <- aggregate( v1 ~ c1 + c2, data = df, sum)
    v2 <- aggregate( v2 ~ c1 + c2, data = df, mean)
    out <- merge(v1, v2, by=c("c1","c2"))
    > out
    #   c1 c2 v1   v2
    # 1  A  A 11 24.5
    # 2  A  B  4 22.5
    # 3  B  A  9 39.0
    # 4  B  B 12 62.5
    

    **Edit:** I’d propose that you use data.table as it makes things really easy:

    require(data.table)
    dt <- data.table(df)
    dt.out <- dt[, list(s.v1=sum(v1), m.v2=mean(v2)), 
                      by=c("c1","c2")]
    > dt.out
    
    #    c1 c2 s.v1 m.v2
    # 1:  A  A   11 24.5
    # 2:  A  B    4 22.5
    # 3:  B  A    9 39.0
    # 4:  B  B   12 62.5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had the following idea: Say we have a webapp written using django which
Let's say I had the following table: id | name | points ------------------- 1
Let's say I had the following table: id num_votes total_rating ------------------------------- 1 20 30
Say I had the following HTML: <h2>Heading <span></span></h2> <ul> <li><a href=# title=I am wanting
Say for example you had a shell script that utilises inotifywait in the following
Let's say we have following table. UserId | Message -------|------------- 1 | Hi, have
Let's say I had a fresh MVC site and I wanted to implement URLs
Let's say I had a MySQL database with the following five records in a
How can I get a sum for all the columns in a datatable? Say
Say I have the following table: ID Value -- ---- 1 1 1 1

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.