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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:18:51+00:00 2026-05-31T19:18:51+00:00

I want to loop through the rows of a data.table and calculate an average

  • 0

I want to “loop through” the rows of a data.table and calculate an average for each row. The average should be calculated based on the following mechanism:

  1. Look up the identifier ID in row i (ID(i))
  2. Look up the value of T2 in row i (T2(i))
  3. Calculate the average over the Data1 values in all rows j, which meet these two criteria: ID(j) = ID(i) and T1(j) = T2(i)
  4. Enter the calculated average in the column Data2 of row i

     DF = data.frame(ID=rep(c("a","b"),each=6), 
                 T1=rep(1:2,each=3), T2=c(1,2,3), Data1=c(1:12))
     DT = data.table(DF)
     DT[ , Data2:=NA_real_]
         ID T1 T2  Data1 Data2
    [1,]  a  1  1     1    NA
    [2,]  a  1  2     2    NA
    [3,]  a  1  3     3    NA
    [4,]  a  2  1     4    NA
    [5,]  a  2  2     5    NA
    [6,]  a  2  3     6    NA
    [7,]  b  1  1     7    NA
    [8,]  b  1  2     8    NA
    [9,]  b  1  3     9    NA
    [10,] b  2  1    10    NA
    [11,] b  2  2    11    NA
    [12,] b  2  3    12    NA
    

For this simple example the result should look like this:

      ID T1 T2  Data1 Data2
[1,]  a  1  1     1    2
[2,]  a  1  2     2    5
[3,]  a  1  3     3    NA
[4,]  a  2  1     4    2
[5,]  a  2  2     5    5
[6,]  a  2  3     6    NA
[7,]  b  1  1     7    8
[8,]  b  1  2     8    11
[9,]  b  1  3     9    NA
[10,] b  2  1    10    8
[11,] b  2  2    11    11
[12,] b  2  3    12    NA

I think one way of doing this would be to loop through the rows, but I think that is inefficient. I’ve had a look at the apply() function, but I’m sure if it would solve my problem. I could also use data.frame instead of data.table if this would make it much more efficient or much easier. The real dataset contains approximately 1 million rows.

  • 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-05-31T19:18:52+00:00Added an answer on May 31, 2026 at 7:18 pm

    The rule of thumb is to aggregate first, and then join to that.

    agg = DT[,mean(Data1),by=list(ID,T1)]
    setkey(agg,ID,T1)
    DT[,Data2:={JT=J(ID,T2);agg[JT,V1][[3]]}]
          ID T1 T2 Data1 Data2
     [1,]  a  1  1     1     2
     [2,]  a  1  2     2     5
     [3,]  a  1  3     3    NA
     [4,]  a  2  1     4     2
     [5,]  a  2  2     5     5
     [6,]  a  2  3     6    NA
     [7,]  b  1  1     7     8
     [8,]  b  1  2     8    11
     [9,]  b  1  3     9    NA
    [10,]  b  2  1    10     8
    [11,]  b  2  2    11    11
    [12,]  b  2  3    12    NA
    

    As you can see it’s a bit ugly in this case (but will be fast). It’s planned to add drop which will avoid the [[3]] bit, and maybe we could provide a way to tell [.data.table to evaluate i in calling scope (i.e. no self join) which would avoid the JT= bit which is needed here because ID is in both agg and DT.

    keyby has been added to v1.8.0 on R-Forge so that avoids the need for the setkey, too.

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

Sidebar

Related Questions

I want to loop through a database of documents and calculate a pairwise comparison
I have a listbox and I want to loop through each of the items
I have a function that is to run through each row of a table,
I want to loop through the controls on a UserControl and set a property
Introduction: I want to loop through XML files with flexible categories structure. Problem: I
In C, I want to loop through an array in this order for(int z
Suppose I have a vector<int> myvec and I want to loop through all of
I have the below but want to only loop through lines in #lines which
If I have a self-referential Topic class and I want to recursively loop through
I want to use nokogiri to loop through a html and create an object

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.