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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:09:13+00:00 2026-05-29T17:09:13+00:00

I have a data.table with columns 2 through 20 as strings with spaces (e.g.,

  • 0

I have a data.table with columns 2 through 20 as strings with spaces (e.g., “Species Name”). I want to run str_replace() on all those columns simultaneously so all the “Species Name” become “Species_Name”. I can either do:

data.table(apply(as.data.frame(dt[,2:dim(dt)[2], with=F]), 2, 
                               function(x){ str_replace(x," ","_") }))

or if I keep it as a data.table object, then I can do this one column at a time:

dt[,SpeciesName := str_replace(SpeciesName, " ", "_")

How do I do this for all columns 2 through the end similar to the one of the above?

  • 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-29T17:09:14+00:00Added an answer on May 29, 2026 at 5:09 pm

    Completely rewritten on 2015-11-24, to fix an error in previous versions.

    Also added more modern options on 2019-09-27

    You have a few options.

    1. Process all of the target columns with an embedded call to
      lapply(), using := to assign the modified values in place. This
      relies on :=‘s very handy support for simultaneous assignment to several column named on its LHS.

    2. Use a for loop to run through the target columns one at a time,
      using set() to modify the value of each one in turn.

    3. Use a for loop to iterate over multiple “naive” calls
      to [.data.table(), each one of which modifies a single column.

    These methods all seem about equally fast, so which one you use will
    be mostly a matter of taste. (1) is nicely compact and
    expressive. It’s what I most often use, though you may find (2)
    easier to read. Because they process and modify the columns one at a time, (2) or (3) will have an advantage in the rare situation in which your data.table is so large that you are in danger of running up against limits
    imposed by your R session’s available memory.

    library(data.table)
    
    ## Create three identical 1000000-by-20 data.tables
    DT1 <- data.table(1:1e6,
               as.data.table(replicate(1e6, paste(sample(letters, nr, TRUE),
                                                 sample(letters, nr, TRUE)))))
    cnames <- c("ID", paste0("X", 1:19))
    setnames(DT1, cnames)
    DT2 <- copy(DT1); DT3 <- copy(DT1)
    
    ## Method 1
    system.time({
    DT1[, .SDcols=cnames[-1L], cnames[-1L] := 
      lapply(.SD, function(x) gsub(" ", "_", x, fixed=TRUE)), ]
    })
    ##   user  system elapsed 
    ##  10.90    0.11   11.06 
    
    ## Method 2
    system.time({
        for(cname in cnames[-1]) {
            set(DT2, j=cname, value=gsub(" ", "_", DT2[[cname]], fixed=TRUE))
        }
    })
    ##   user  system elapsed 
    ##  10.65    0.05   10.70 
    
    ## Method 3
    system.time({
        for(cname in cnames[-1]) {
            DT3[ , (cname) := gsub(" ", "_", get(cname), fixed=TRUE)]
        }
    })
    ##   user  system elapsed 
    ##  10.33    0.03   10.37 
    

    For more details on set() and :=, read their help page, gotten by typing ?set or ?":=".

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

Sidebar

Related Questions

I have a Datatable with Id(guid) and Name(string) columns. I traverse through the data
I have table in data base name train delay, with columns train number(int), DelayTime(int),
I have produced a data table. All the columns are sortable. It has a
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 table containing two columns in SQL that I want to extract
I have an excel sheet similar to : I want to read data columns
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

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.