Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Completely rewritten on 2015-11-24, to fix an error in previous versions.</strong></p> <p>You have a few options.</p> <ol> <li><p>Process all of the target columns with an embedded call to <code>lapply()</code>, using <code>:=</code> to assign the modified values in place. This relies on <code>:=</code>'s very handy support for simultaneous assignment to several column named on its LHS.</p></li> <li><p>Use a <code>for</code> loop to run through the target columns one at a time, using <code>set()</code> to modify the value of each one in turn.</p></li> <li><p>Use a <code>for</code> loop to iterate over multiple "naive" calls to <code>[.data.table()</code>, each one of which modifies a single column.</p></li> </ol> <p>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.</p> <pre><code>library(data.table) ## Create three identical 1000000-by-20 data.tables DT1 &lt;- data.table(1:1e6, as.data.table(replicate(1e6, paste(sample(letters, nr, TRUE), sample(letters, nr, TRUE))))) cnames &lt;- c("ID", paste0("X", 1:19)) setnames(DT1, cnames) DT2 &lt;- copy(DT1); DT3 &lt;- copy(DT1) ## Method 1 system.time({ DT1[, cnames[-1] := lapply(DT1[,cnames[-1],with=FALSE], function(x) gsub(" ", "_", x))] }) ## 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]])) } }) ## user system elapsed ## 10.65 0.05 10.70 ## Method 3 system.time({ for(cname in cnames[-1]) { DT3[ , cname := gsub(" ", "_", DT3[[cname]]), with=FALSE] } }) ## user system elapsed ## 10.33 0.03 10.37 </code></pre> <p>For more details on <code>set()</code> and <code>:=</code>, read their help page, gotten by typing <code>?set</code> or <code>?":="</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload