Tutorial by Examples

To copy a data.frame as a data.table, use as.data.table or data.table: DF = data.frame(x = letters[1:5], y = 1:5, z = (1:5) > 3) DT <- as.data.table(DF) # or DT <- data.table(DF) This is rarely necessary. One exception is when using built-in datasets like mtcars, which must be copi...
There is a constructor of the same name: DT <- data.table( x = letters[1:5], y = 1:5, z = (1:5) > 3 ) # x y z # 1: a 1 FALSE # 2: b 2 FALSE # 3: c 3 FALSE # 4: d 4 TRUE # 5: e 5 TRUE Unlike data.frame, data.table will not coerce strings to factors by default: sa...
We can read from a text file: dt <- fread("my_file.csv") Unlike read.csv, fread will read strings as strings, not as factors by default. See the [topic on fread][need_a_link] for more examples.
For efficiency, data.table offers a way of altering a data.frame or list to make a data.table in-place: # example data.frame DF = data.frame(x = letters[1:5], y = 1:5, z = (1:5) > 3) # modification setDT(DF) Note that we do not <- assign the result, since the object DF has been modifi...
# example data DT1 = data.table(x = letters[1:2], y = 1:2, z = (1:2) > 3) Due to the way data.tables are manipulated, DT2 <- DT1 will not make a copy. That is, later modifications to the columns or other attributes of DT2 will affect DT1 as well. When you want a real copy, use DT2 = copy(...

Page 1 of 1