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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:36:34+00:00 2026-05-24T12:36:34+00:00

data.table objects now have a := operator. What makes this operator different from all

  • 0

data.table objects now have a := operator. What makes this operator different from all other assignment operators? Also, what are its uses, how much faster is it, and when should it be avoided?

  • 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-24T12:36:34+00:00Added an answer on May 24, 2026 at 12:36 pm

    Here is an example showing 10 minutes reduced to 1 second (from NEWS on homepage). It’s like subassigning to a data.frame but doesn’t copy the entire table each time.

    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,1] <- i)
         user  system elapsed 
      287.062 302.627 591.984 
    
    system.time(for (i in 1:1000) DT[i,V1:=i])
         user  system elapsed 
        1.148   0.000   1.158     ( 511 times faster )
    

    Putting the := in j like that allows more idioms :

    DT["a",done:=TRUE]   # binary search for group 'a' and set a flag
    DT[,newcol:=42]      # add a new column by reference (no copy of existing data)
    DT[,col:=NULL]       # remove a column by reference
    

    and :

    DT[,newcol:=sum(v),by=group]  # like a fast transform() by group
    

    I can’t think of any reasons to avoid := ! Other than, inside a for loop. Since := appears inside DT[...], it comes with the small overhead of the [.data.table method; e.g., S3 dispatch and checking for the presence and type of arguments such as i, by, nomatch etc. So for inside for loops, there is a low overhead, direct version of := called set. See ?set for more details and examples. The disadvantages of set include that i must be row numbers (no binary search) and you can’t combine it with by. By making those restrictions set can reduce the overhead dramatically.

    system.time(for (i in 1:1000) set(DT,i,"V1",i))
         user  system elapsed 
        0.016   0.000   0.018
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to delete some rows from a data table. I've heard that it
Data table structure is: id1,id2,id3,id4,... (some other fields). I want to create summary query
I am displaying a scrolled data table in a web page. This table has
I have a two-dimensional array (of Strings) which make up my data table (of
I have a small app/site on a dev server with a data table in
I have Silverlight application that retrieves data from the database through a WCF Service.
I have two data access objects that are reverse generated and jar'ed up for
I have two database tables. One table stores data for a commitment that a
I have C# .NET code with a DataSet (System.Data.DataSet) containing one table of data,
I have a service that calls database to retrieve data; this service will return

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.