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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:32:10+00:00 2026-06-17T19:32:10+00:00

I have two data.tables, main and metrics , both keyed by cid I want

  • 0

I have two data.tables, main and metrics, both keyed by cid
I want to add to table main the average of each of several values located in metrics.

However, I would like to filter by code, only averaging those rows in metrics with a given code.

> metrics
    cid code  DZ value1 value2
1: 1001    A 101      8     21
2: 1001    B 102     11     26
3: 1001    A 103     17     25
4: 1002    A 104     25     39
5: 1002    B 105      6     30
6: 1002    A 106     23     40
7: 1003    A 107     27     32
8: 1003    B 108     16     37
9: 1003    A 109     14     42

# DESIRED OUTPUT
> main
    cid  A.avg.val1   A.avg.val2    B.avg.val1      B.avg.val2    
1: 1001    12.5         23.0            11              26                      
2: 1002    24.0         39.5             6              30            
3: 1003    20.5         37.0            16              37            



#  SAMPLE DATA
set.seed(1)
main <- data.table(cid=1e3+1:3, key="cid")
metrics <- data.table(cid=rep(1e3+1:3, each=3), code=rep(c("A", "B", "A"), 3), DZ=101:109, value1=sample(30, 9), value2=sample(20:50, 9), key="cid")
code.filters <- c("A", "B")

These lines get the desired output, but I am having difficulty assigning the new col back into main. (also, doing it programatically would be preferred).

main[metrics[code==code.filters[[1]]]][,  list(mean(c(value1))), by=cid]
main[metrics[code==code.filters[[1]]]][,  list(mean(c(value2))), by=cid]
main[metrics[code==code.filters[[2]]]][,  list(mean(c(value1))), by=cid]
main[metrics[code==code.filters[[1]]]][,  list(mean(c(value2))), by=cid]

Additionally, can someone explain why the following line only takes the last value in each group?

main[metrics[ code=="A"],  A.avg.val1 := mean(c(value1))]
  • 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-17T19:32:11+00:00Added an answer on June 17, 2026 at 7:32 pm

    working off of @Arun’s answer, the following gets the desired results:

    invisible( 
    sapply(code.filters, function(cf)
        main[metrics[code==cf, list(avgv1 = mean(value1), avgv2 = mean(value2)), by=cid],
          paste0(cf, c(".avg.val1", ".avg.val2")) :=list(avgv1, avgv2)]
    ))
    
    > main
        cid A.avg.val1 A.avg.val2 B.avg.val1 B.avg.val2
    1: 1001       12.5       23.0         11         26
    2: 1002       24.0       39.5          6         30
    3: 1003       20.5       37.0         16         37
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server, I have two tables. One main table with all the data
So my situation is this. I have two tables. A main table where data
I have two tables. Table1 is our main table of data and Table2 has
I have two Tables: Table 1: Questions : QuestionId NUMERIC Title TEXT Test Data
I have two tables set up in phpmyadmin- table userid and table data. The
I have two tables, a main one, and one that supports the main table,
I have two tables a main table and a work in progress table. Any
I want to append data of two tables in a resultset. I have tried
i have two data tables with the same structure the first one has one
I have a database with two tables: data and file . file_id is 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.