The official Cheat Sheet for the DataCamp course
DATA ANALYSIS THE DATA.TABLE WAY
General form: DT[i , j , by
]
“Take DT, subset rows using i
, then calculate j grouped by by ”
ADDING/UPDATING COLUMNS BY REFERENCE IN USING := What?
Example
Notes
Output
Adding/updating a column by
reference using := in one line. Watch out : extra assignment (DT <- DT [...]) is redundant. DT [, V1 := round (exp (V1),2)] Column V1 is updated by what is after :=. Returns the result invisibly.
Column V1 went from: [1] 1 2 1
2 … to [1] 2.72 7.39 2.72 7.39 …
Adding/updating several
columns by reference using :=.
DT [, c ("V1","V2") := list (round (exp (V1),2), LETTERS [4:6])]
Column V1 and V2 are updated by what is after :=. Returns the result invisibly. Column V1 changed as above.
Column V2 went from: [1] "A"
"B" "C" "A" "B" "C" … to: [1] "D" "E" "F" "D" "E" "F" …
Using functional :=.
DT [, ':=' (V1 =
round (exp (V1),2), V2 = LETTERS [4:6])][]
Another way to write the same line as above this one, but easier to write
comments side-by-side. Also, when [] is added the result is printed to the screen.
Same changes as line above this one, but the result is printed to the screen because of the [] at the end of the statement.
Remove a column instantly using :=.
DT [, V1 := NULL ]
Removes column V1.
Returns the result invisibly. Column V1 became NULL . Remove several columns instantly using :=.
DT [, c ("V1","V2") := NULL ]
Removes columns V1 and V2. Returns the result invisibly. Col-umn V1 and V2 became NULL .
Wrap the name of a variable
which contains column names in
parenthesis to pass the contents of that variable to be deleted.
Cols.chosen = c ("A","B")
DT [, Cols.chosen := NULL ]
Watch out : this deletes the column with column name Cols.chosen . Returns the result invisibly.
Column with name Cols.chosen became NULL .
DT [, (Cols.chosen ) := NULL ]
Deletes the columns specified in the variable Cols.chosen (V1 and V2).
Returns the result invisibly.
Columns V1 and V2 became NULL .
J