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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:27:37+00:00 2026-06-15T12:27:37+00:00

A data.table novice question. I would like to transform a set of columns in

  • 0

A data.table novice question.
I would like to transform a set of columns in a data.table by applying a mathematical formula to them. The set of columns must exclude 1 or more of the total number of columns.

In data.frame terms I would do:

data(iris)
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

iris[, -5] <- iris[, -5] * 1e3
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1         5100        3500         1400         200  setosa
2         4900        3000         1400         200  setosa
3         4700        3200         1300         200  setosa
4         4600        3100         1500         200  setosa
5         5000        3600         1400         200  setosa
6         5400        3900         1700         400  setosa

I know how to select multiple columns in a data.table:

iris.dt <- data.table(iris)
head(iris.dt[, -5, with = FALSE])

or even:

head(iris.dt[, !"Species", with = FALSE])

How to actually transform those selected columns taking advantage of data.table pass-by-reference?

  • 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-15T12:27:39+00:00Added an answer on June 15, 2026 at 12:27 pm

    What about using the .SDCols argument along with assignment by reference (:=):

    DT <- data.table(iris)
    DT[, c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width")
       :=lapply(.SD, function(x) x*1000), .SDcols=1:4]
    # Alternatively you can grab the names the usual way:
    # DT[, names(DT)[1:4] := lapply(.SD, function(x) x*1000), .SDcols=1:4]
    DT
    #      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
    #   1:         5100        3500         1400         200    setosa
    #   2:         4900        3000         1400         200    setosa
    #   3:         4700        3200         1300         200    setosa
    #   4:         4600        3100         1500         200    setosa
    #   5:         5000        3600         1400         200    setosa
    #  ---                                                            
    # 146:         6700        3000         5200        2300 virginica
    # 147:         6300        2500         5000        1900 virginica
    # 148:         6500        3000         5200        2000 virginica
    # 149:         6200        3400         5400        2300 virginica
    # 150:         5900        3000         5100        1800 virginica
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

data table contain column named as Fld_primary. this column contain value like 0.00 it
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have a data table with many rows and columns. How can I display
Sorry for the novice question! I have a table called cities in which I
I want to insert data into a table like the following: emp_id emp_data ----------------------------
I apologize if this may seem like somewhat of a novice question (which it
The data.table package is very helpful in terms of speed. But I am having
The data.table package provides many of the same table handling methods as SQL. If
I have a sample data table(id, name, date_create) table(1, 'Iphone 4S', '2012-06-18 17:19:05') //
I have a JSF data table <h:dataTable id=memberTable value=#{bean.pList} border=0 rowClasses=rowEven rowOdd var=item> <h:column

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.