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

  • Home
  • SEARCH
  • 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 6902453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:48:00+00:00 2026-05-27T07:48:00+00:00

I have a data.table DT with a column named RF and many columns with

  • 0

I have a data.table DT with a column named RF and many columns with an underline _in it. I want to loop through all those columns with an
underline and subtract the RF column from it. However, I’m stuck. It seems that everything on the RHS of the
:= operator in a data.table does not work with dynamic variables.

Here is my DT and the desired output (hardcoded):

library(data.table)
DT <- data.table(RF  = 1:10,
                 S_1 = 11:20,
                 S_2 = 21:30)
#Desired output
DT[ , S_1 := S_1 - RF]
DT[ , S_2 := S_2 - RF]
DT
      RF S_1 S_2
 [1,]  1  10  20
 [2,]  2  10  20
 [3,]  3  10  20
...

However, I want this to be more flexible, i.e. loop through every column with “_” in its name and subtract RF:

#1. try: Does not work; Interestingly, the i on the LHS of := is interpreted as the column i, but on the RHS of
#:= it is interpreted as 2 and 3, respectively
for (i in grep("_", names(DT))){
  DT[ , i:= i - 1, with=FALSE]
}
DT
          RF  S_1 S_2
 [1,]  1   1   2
 [2,]  2   1   2
 [3,]  3   1   2
...

#2. try: Work with parse and eval
for (i in grep("_", names(DT), value=TRUE)){
  DT[ , eval(parse(text=i)):= eval(parse(text=i)) - RF]
}
#Error in eval(expr, envir, enclos) : object 'S_1' not found

Any hints how to do that would be great.

EDIT: As soon as I posted the question, I thought to myself: Why are you working with the := operator in the first place, and sure enough, I just realized I don’t have to. This does work and doesn’t need a loop:

DT[, grep("_", names(DT)), with=FALSE] - DT[, RF]

Sorry for that. However, I leave the question open because I’m still interested on why my approach with the := operator doesn’t work. So maybe someone can help me there.

  • 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-27T07:48:00+00:00Added an answer on May 27, 2026 at 7:48 am

    You were on the right track with your second attempt. Here is an approach that uses substitute to build the expression that gets passed in as the 'j' argument in DT[ , j ].

    for (i in grep("_", names(DT), value=TRUE)){
        e <- substitute(X := X - RF, list(X = as.symbol(i)))
        DT[ , eval(e)]
    }
    DT
    #     RF S_1 S_2
    # [1,]  1  10  20
    # [2,]  2  10  20
    # [3,]  3  10  20
    # [4,]  4  10  20
    # [5,]  5  10  20
    

    You could also use an LHS expression rather than a symbol :

    for (i in grep("_", names(DT), value=TRUE))
        DT[, (i) := get(i)-RF]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table in data base name train delay, with columns train number(int), DelayTime(int),
I have a Datatable with Id(guid) and Name(string) columns. I traverse through the data
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
If I have a table column with data and create an index on this
I have a table of data that I need to dynamically add a column
The table columns have the data type BLOB and CLOB. What are the corresponding
I have some data I am querying. The table is composed of two columns
I have a database table named call with columns call_time, location, emergency_type and there
I have a table columns:Name,Role,Dept A person can have many roles. So while retrieving
I have datatable with column name tag and 100 rows of data.I need to

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.