# example data
DT = as.data.table(mtcars, keep.rownames = TRUE)
Use the :=
operator inside j
to create new columns or modify existing ones:
DT[, mpg_sq := mpg^2]
Use the i
argument to subset to rows "where" edits should be made:
DT[1:3, newvar := "Hello"]
As in a data.frame, we can subset using row numbers or logical tests. It is also possible to use [a "join" in i
when modifying][need_a_link].
Remove columns by setting to NULL
:
DT[, mpg_sq := NULL]
Note that we do not <-
assign the result, since DT
has been modified in-place.
Add multiple columns by using the :=
operator's multivariate format:
DT[, `:=`(mpg_sq = mpg^2, wt_sqrt = sqrt(wt))]
# or
DT[, c("mpg_sq", "wt_sqrt") := .(mpg^2, sqrt(wt))]
The .()
syntax is used when the right-hand side of LHS := RHS
is a list of columns.
If the columns are dependent and must be defined in sequence, some ways to do that are:
DT[, c("mpg_sq", "mpg2_hp") := .(temp1 <- mpg^2, temp1/hp)]
# or
DT[, c("mpg_sq", "mpg2_hp") := {temp1 = mpg^2; .(temp1, temp1/hp)}]
For dynamically-determined column names, use parentheses:
vn = "mpg_sq"
DT[, (vn) := mpg^2]
set
Columns can also be modified with set
for a small reduction in overhead, though this is rarely necessary:
set(DT, j = "hp_over_wt", v = mtcars$hp/mtcars$wt)