Data table code snippets

Data table code snippets

Tags
Code
Created
Dec 16, 2020 6:14 AM

Remove rows with NA's using data table

DT[complete.cases(DT),]

DPLYR Functions and their equivalents

n() -> .N

Group by Mutate vs Summarize

# Mutate
DT <- DT[,`:=`(a= mean(x), b=sd(y),.(group_var1, group_var2)]

# Summarize
DT <- DT[,.(a= mean(x), b=sd(y),.(group_var1, group_var2)]

Rank per group based on a column

DT[, rnk:=frank(order_var), .(group_var)]
# descending
DT[, rnk:=frank(-order_var), .(group_var)]

Remove a column

DT[, col:=NULL]