Download Data Transformation with Data Table Cheat Sheet and more Cheat Sheet Data Structures and Algorithms in PDF only on Docsity! Basics CC BY SA Erik Petrovski โขUpdated: 2018-09 Data Transformation with data.table : : CHEAT SHEET Manipulate columns with j Functions for data.tables data.table is an extremely fast and memory efficient package for transforming data in R. It works by converting Rโs native data frame objects into data.tables with new and enhanced functionality. The basics of working with data.tables are: dt[i, j, by] Take data.table dt, subset rows using i, and manipulate columns with j, grouped according to by. data.tables are also data frames โ functions that work with data frames therefore also work with data.tables. data.table(a = c(1, 2), b = c("a", "b")) โ create a data.table from scratch. Analogous to data.frame(). setDT(df)* or as.data.table(df) โ convert a data frame or a list to a data.table. Create a data.table dt[1:2, ] โ subset rows based on row numbers. dt[a > 5, ] โ subset rows based on values in one or more columns. Subset rows using i LOGICAL OPERATORS TO USE IN i < <= is.na() %in% | %like% > >= !is.na() ! & %between% dt[, c(2)] โ extract column(s) by number. Prefix column numbers with โ-โ to drop. dt[, .(b, c)] โ extract column(s) by name.b cb c a 6 a 2 6 5 EXTRACT dt[, .(x = sum(a))] โ create a data.table with new columns based on the summarized values of rows. Summary functions like mean(), median(), min(), max(), etc. may be used to summarize rows. dt[, .(c = sum(b)), by = a] โ summarize rows within groups. dt[, c := sum(b), by = a] โ create a new column and compute rows within groups. dt[, .SD[1], by = a] โ extract first row of groups. dt[, .SD[.N], by = a] โ extract last row of groups. COMMON GROUPED OPERATIONS COMPUTE COLUMNS* c 3 3 dt[, c := 1 + 2] โ compute a column based on an expression. setorder(dt, a, -b) โ reorder a data.table according to specified columns. Prefix column names with โ-โ for descending order. a b 1 2 1 1 2 2 a b 1 2 2 2 1 1 REORDER a 2 1 dt[a == 1, c := 1 + 2] โ compute a column based on an expression but only for a subset of rows. xa SUMMARIZE a c 2 NA 1 3 Group according to by aa a dt[, j, by = .(a)] โ group rows by values in specified column(s). dt[, j, keyby = .(a)] โ group and simultaneously sort rows according to values in specified column(s). Chaining dt[โฆ][โฆ] โ perform a sequence of data.table operations by chaining multiple โ[]โ. * SET FUNCTIONS AND := data.tableโs functions prefixed with โsetโ and the operator โ:=โ work without โ<-โ to alter data without making copies in memory. E.g. the more efficient โsetDT(df)โ is analogous to โdf <- as.data.table(df)โ. c d 1 2 1 2 dt[, `:=`(c = 1 , d = 2)] โ compute multiple columns based on separate expressions. DELETE COLUMN c dt[, c := NULL] โ delete a column. CONVERT COLUMN TYPE b 1.5 2.6 b 1 2 dt[, b := as.integer(b)] โ convert the type of a column using as.integer(), as.numeric(), as.character(), as.Date(), etc..