Advanced Data Wrangling with data.table in R
Instruction: Demonstrate the use of data.table for advanced data wrangling tasks in R, including complex filtering, aggregation, and joining operations.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
data.table uses DT[i, j, by] for filtering, selecting/computing and grouping. Reference updates use := or set functions; ordinary row filtering does not automatically update the input by reference.
library(data.table) sales <- data.table(id = 1:4, region = c("N", "N", "S", "X"), amount = c(10, NA_real_, 30, 40)) regions <- data.table(region = c("N", "S"), label = c("North", "South")) filtered <- sales[!is.na(amount) & amount >= 20] summary <- sales[, .(rows = .N, observed = sum(!is.na(amount)),...