Scalable Data Analysis with data.table

Instruction: Using data.table in R, count distinct nonmissing user IDs per day for dates Jan 1, Jan 1, Jan 1, Jan 1, Jan 2 (2026) and IDs u1,u1,u2,missing,u1. Explain why .N is not DAU, then discuss reading, grouping, reference semantics and memory limits for larger data.

Context: This question evaluates the candidate's skills in handling large datasets with data.table, emphasizing efficiency and performance.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

In data.table, .N counts rows. To count distinct active users per day, use uniqueN(user_id) after defining a qualifying event, timezone and missing-ID policy.

library(data.table) DT <- data.table( day = as.IDate(c("2026-01-01", "2026-01-01", "2026-01-01", "2026-01-01", "2026-01-02")), user_id = c("u1", "u1", "u2", NA, "u1") ) dau <- DT[!is.na(user_id), .(dau = uniqueN(user_id)), by = day][order(day)]...

Related Questions