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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:13:25+00:00 2026-06-07T04:13:25+00:00

Is there a way to prevent data.table to print the new data.table after assigning

  • 0

Is there a way to prevent data.table to print the new data.table after assigning a new column by reference? I gather standard behaviour is

library(data.table)
example(data.table)
DT
#    x y  v
# 1: a 1 42
# 2: a 3 42
# 3: a 6 42
# 4: b 1 11
# 5: b 3 11
# 6: b 6 11
# 7: c 1  7
# 8: c 3  8
# 9: c 6  9

DT[,z:=1:nrow(DT)]

#    x y  v z
# 1: a 1 42 1
# 2: a 3 42 2
# 3: a 6 42 3
# 4: b 1 11 4
# 5: b 3 11 5
# 6: b 6 11 6
# 7: c 1  7 7
# 8: c 3  8 8
# 9: c 6  9 9

i.e. the table is printed to screen after assignment. is there a way to stop data.table from showing the new table after assigning the new column z? I know I can stop this behaviour by saying

DT <- copy(DT[,z:=1:nrow(DT)])

but that is defeating the purpose of := (which is designed to avoid copies).

  • 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-07T04:13:26+00:00Added an answer on June 7, 2026 at 4:13 am

    Since <-.data.table doesn’t make a copy, you can use <-:

    Create a data.table object:

    library(data.table)
    di <- data.table(iris)
    

    Create a new column:

    di <- di[, z:=1:nrow(di)]
    di
    
    #       Sepal.Length Sepal.Width Petal.Length Petal.Width Species  z
    #  [1,]          5.1         3.5          1.4         0.2  setosa  1
    #  [2,]          4.9         3.0          1.4         0.2  setosa  2
    #  [3,]          4.7         3.2          1.3         0.2  setosa  3
    #  [4,]          4.6         3.1          1.5         0.2  setosa  4
    #  [5,]          5.0         3.6          1.4         0.2  setosa  5
    #  [6,]          5.4         3.9          1.7         0.4  setosa  6
    #  [7,]          4.6         3.4          1.4         0.3  setosa  7
    #  [8,]          5.0         3.4          1.5         0.2  setosa  8
    #  [9,]          4.4         2.9          1.4         0.2  setosa  9
    # [10,]          4.9         3.1          1.5         0.1  setosa 10
    # First 10 rows of 150 printed. 
    

    It is also worth remembering that R only prints the value of an object in interactive mode.

    So, in batch mode, you can simply use:

    di[, z:=1:nrow(di)]
    

    This will not produce any output when run as a script in batch mode.


    Further info from Matthew Dowle:

    Also see FAQ 2.21 and 2.22 :

    2.21 Why does DT[i,col:=value] return the whole of DT? I expected either no visible value (consistent with <-), or a message or return value containing how many rows were updated. It isn’t obvious that the data has indeed been updated by reference.

    So that compound syntax can work; e.g., DT[i,done:=TRUE][,sum(done)]. The number of rows updated is returned when verbosity is on, either on a per query basis or globally using options(datatable.verbose=TRUE).

    2.22 Ok, but can’t the return value of DT[i,col:=value] be returned invisibly, then?

    • We tried to but R internally forces visibility on for [. The value of
      FunTab’s eval column (see src/main/names.c) for [ is 0 meaning force
      R_Visible on (see R-Internals section 1.6). Therefore, when we tried
      invisible() or setting R_Visible to 0 directly ourselves, eval in
      src/main/eval.c would force it on again.
    • After getting used to this behaviour, you might grow to prefer it (we have). After all, how many times do we subassign using <- and then immediately look at the data to check it’s ok?
    • We can mix := into a j which also returns data; a mixed update and select in one query. To detect whether j solely updates (and then behave dierently) could be confusing.

    Second update from Matthew Dowle:

    We have now found a solution and v1.8.3 no longer prints the result when := is used. We will update FAQ 2.21 and 2.22.

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

Sidebar

Related Questions

Is there a quick way to prevent insertion of repeated data into a table?
Is there a way to prevent users from getting into SQL Server Management Studio
Is there a way to prevent Castle Windsor from automatically injecting dependencies into properties
Is there a way to prevent a NSDrawer from being resized? I've tried setting
Is there a way to prevent empty elements of the form <myElement/> being used
Is there a way to prevent a user viewing an file but still use
Is there a way to prevent a component from rendering in Flex (to save
Is there a way to prevent developers from committing code when certain unsafe or
Is there any way to prevent or detect a man-in-the-middle attack over plain HTTP?
HI, Is there a way to prevent a particular dll in C# being opened

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.