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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:51:08+00:00 2026-06-01T17:51:08+00:00

Using a data.table, which would be the fastest way to sweep out a statistic

  • 0

Using a data.table, which would be the fastest way to “sweep” out a statistic across a selection of columns?

Starting with (considerably larger versions of ) DT

p <- 3
DT <- data.table(id=c("A","B","C"),x1=c(10,20,30),x2=c(20,30,10))
DT.totals <- DT[, list(id,total = x1+x2) ]

I’d like to get to the following data.table result by indexing the target columns (2:p) in order to skip the key:

    id  x1  x2
[1,]    A   0.33    0.67
[2,]    B   0.40    0.60
[3,]    C   0.75    0.25
  • 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-01T17:51:10+00:00Added an answer on June 1, 2026 at 5:51 pm

    I believe that something close to the following (which uses the relatively new set() function) will be quickest:

    DT <- data.table(id = c("A","B","C"), x1 = c(10,20,30), x2 = c(20,30,10))
    total <- DT[ , x1 + x2]
    
    rr <- seq_len(nrow(DT))
    for(j in 2:3) set(DT, rr, j, DT[[j]]/total) 
    DT
    #      id        x1        x2
    # [1,]  A 0.3333333 0.6666667
    # [2,]  B 0.4000000 0.6000000
    # [3,]  C 0.7500000 0.2500000
    

    FWIW, calls to set() takes the following form:

    # set(x, i, j, value), where: 
    #     x is a data.table 
    #     i contains row indices
    #     j contains column indices 
    #     value is the value to be assigned into the specified cells
    

    My suspicion about the relative speed of this, compared to other solutions, is based on this passage from data.table’s NEWS file, in the section on changes in Version 1.8.0:

    o   New function set(DT,i,j,value) allows fast assignment to elements
        of DT. Similar to := but avoids the overhead of [.data.table, so is
        much faster inside a loop. Less flexible than :=, but as flexible
        as matrix subassignment. Similar in spirit to setnames(), setcolorder(),
        setkey() and setattr(); i.e., assigns by reference with no copy at all.
    
            M = matrix(1,nrow=100000,ncol=100)
            DF = as.data.frame(M)
            DT = as.data.table(M)
            system.time(for (i in 1:1000) DF[i,1L] <- i)   # 591.000s
            system.time(for (i in 1:1000) DT[i,V1:=i])     #   1.158s
            system.time(for (i in 1:1000) M[i,1L] <- i)    #   0.016s
            system.time(for (i in 1:1000) set(DT,i,1L,i))  #   0.027s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which implements a tree using hierarchyid column Sample data: People
I am using javascript to populate data in a table which return everything fine.
I have a table which has columns of data that contain status. Two example
Using a data table from http://datatables.net/ , How can i stop it from overflowing
I am trying to import an excel file into a data table using GemBox
I am using a Windows application for exporting a data table to Excel. It's
I'm using MyFaces 1.1.7 with Facelets and Tomahawk. When creating a regular data table,
I'm trying to reset some input text fields, inside of a data table, using
I am trying to convert following table data into nested array using PHP. I
just i print the two table data so i am using inner join SELECT

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.