> For the complete documentation index, see [llms.txt](https://ondrej-kvasnovsky-2.gitbook.io/handbook-of-hidden-data-scientist/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ondrej-kvasnovsky-2.gitbook.io/handbook-of-hidden-data-scientist/r/data_table.md).

# Data Table

Facts about `data.table`.

* Extends from `data.frame` and therefore should provide the same API.
* Is written in C and is really fast.
* Much faster at subsetting, grouping and updating.

## Hello world

```r
install.packages("data.table")
library(data.table)

years = c(2012, 2013)
average = c(250, 275)
table.values <- data.table(year = years, averageBeerConsumption = average)
```

See all `data.table` tables created in memory.

```r
tables()
```

## Subsetting rows.

Access row on specific index.

```r
table.values[2]
table.values[c(1,2)]
```

Access rows that fulfil a condition.

```r
table.values[table.values$year==2012]
```

## Calculate values from columns

```r
table.values[, sum(averageBeerConsumption)]

table.values[, list(mean(year), sum(averageBeerConsumption))]
```

## Return table of values for a column

```r
table.values[, table(year)]
```

## Add new column

```r
table.values[, volume:=averageBeerConsumption*0.5]
```

## Multiple operations.

```r
table.values[,
    x:={temp <- averageBeerConsumption*year;
log2(temp)
}]
```

## Plyr like operations

```r
table.values[, y:= year<2013]
```

## Grouping by

```r
table.values[, sum:= sum(averageBeerConsumption), by= year]
```

## Count number of occurrences

```r
table.values[, .N, by=year]
```

## Keys

Making table faster by setting the keys

```r
setkey(table.values, year)
```

Then we can join tables by keys.

```r
setkey(table1.values, year)
setkey(table2.values, year)

merge(table1.values, table2.values)
```

## Fast reading

First we create a file that we can use to test speed of reading.

```r
big.file <- data.frame(x=rnorm(1E6), y=rnorm(1E6))

file <- tempfile()

write.table(big.file, file=file, row.names=FALSE, col.names=TRUE, sep="\t", quote=FALSE)
```

Slow approach using `read.table` function.

```r
system.time(read.table(file, header=TRUE, sep="\t"))
```

Faster approach using `fread` function.

```r
system.time(fread(file))
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ondrej-kvasnovsky-2.gitbook.io/handbook-of-hidden-data-scientist/r/data_table.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
