Data table code snippets

Data table code snippets

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]