Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data Transformation with Data Table Cheat Sheet, Cheat Sheet of Data Structures and Algorithms

How to create a data table by a cheat sheet.

Typology: Cheat Sheet

2020/2021

Uploaded on 04/26/2021

jeny
jeny ๐Ÿ‡บ๐Ÿ‡ธ

4.6

(14)

12 documents

Partial preview of the text

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..
Docsity logo



Copyright ยฉ 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved