Copy on Modify

tutorials
tips
Author

Kelly Bodwin

Published

December 1, 2025

library(data.table)
library(palmerpenguins)

Test 1: ordinary dt

dat <- data.table(penguins)
dim(dat)
[1] 344   8
dat[species == "Adelie"]
     species    island bill_length_mm bill_depth_mm flipper_length_mm
      <fctr>    <fctr>          <num>         <num>             <int>
  1:  Adelie Torgersen           39.1          18.7               181
  2:  Adelie Torgersen           39.5          17.4               186
  3:  Adelie Torgersen           40.3          18.0               195
  4:  Adelie Torgersen             NA            NA                NA
  5:  Adelie Torgersen           36.7          19.3               193
 ---                                                                 
148:  Adelie     Dream           36.6          18.4               184
149:  Adelie     Dream           36.0          17.8               195
150:  Adelie     Dream           37.8          18.1               193
151:  Adelie     Dream           36.0          17.1               187
152:  Adelie     Dream           41.5          18.5               201
     body_mass_g    sex  year
           <int> <fctr> <int>
  1:        3750   male  2007
  2:        3800 female  2007
  3:        3250 female  2007
  4:          NA   <NA>  2007
  5:        3450 female  2007
 ---                         
148:        3475 female  2009
149:        3450 female  2009
150:        3750   male  2009
151:        3700 female  2009
152:        4000   male  2009
nrow(dat)
[1] 344

Filtering does not modify in place.

dat[, Big := body_mass_g > 4000]
names(dat)
[1] "species"           "island"            "bill_length_mm"   
[4] "bill_depth_mm"     "flipper_length_mm" "body_mass_g"      
[7] "sex"               "year"              "Big"              

Mutating does modify in place.

dat[, ]
       species    island bill_length_mm bill_depth_mm flipper_length_mm
        <fctr>    <fctr>          <num>         <num>             <int>
  1:    Adelie Torgersen           39.1          18.7               181
  2:    Adelie Torgersen           39.5          17.4               186
  3:    Adelie Torgersen           40.3          18.0               195
  4:    Adelie Torgersen             NA            NA                NA
  5:    Adelie Torgersen           36.7          19.3               193
 ---                                                                   
340: Chinstrap     Dream           55.8          19.8               207
341: Chinstrap     Dream           43.5          18.1               202
342: Chinstrap     Dream           49.6          18.2               193
343: Chinstrap     Dream           50.8          19.0               210
344: Chinstrap     Dream           50.2          18.7               198
     body_mass_g    sex  year    Big
           <int> <fctr> <int> <lgcl>
  1:        3750   male  2007  FALSE
  2:        3800 female  2007  FALSE
  3:        3250 female  2007  FALSE
  4:          NA   <NA>  2007     NA
  5:        3450 female  2007  FALSE
 ---                                
340:        4000   male  2009  FALSE
341:        3400 female  2009  FALSE
342:        3775   male  2009  FALSE
343:        4100   male  2009   TRUE
344:        3775 female  2009  FALSE

Seal of Approval: collapse

seal of approval
partner package
No matching items