# Which is Faster?: R Data Manipulation with data.table vs dplyr

I started my voyage into learning R by taking Datacamp’s online courses.  After finishing courses on data manipulation in both base R and dplyr, I stumbled upon a course on using the data.table library.  I was taken back a bit after learning that data.table using a different syntax than base R.  This was unnerving as I didn’t know what I would gain from learning data manipulation in yet another syntax.  My skepticism, however, changed to optimism once I began working on a rather large dataset a few weeks later.  This dataset (a 43M row set of email opens and clickthroughs), took something like 30-40 minutes to read into R using the base read.csv function.  Instead, I tried using the fread function in data.table.  Low and behold, what took 30-40 minutes using base R took about 5 minutes using fread.  Here’s timing data for a 3M row text file:

The speed improvements are not just limited to reading data.  Manipulations are also faster using data.table.  Here are 2 functions written to group and count rows using the world cities population dataset.

```library(data.table)
library(dplyr)
library(microbenchmark)

df_func <- function(x) {
x %>%
group_by(Country) %>%
summarize(
Count = n()
) %>%
arrange(desc(Count))
}

dt_func <- function(x) {
x[,.(Count = .N), Country][order(-Count)]
}

microbenchmark(df_func(wcp), dt_func(wcp),times = 10)```
{"ft_lang_label":"__label__en","ft_lang_prob":0.9081128,"math_prob":0.800048,"size":2217,"snap":"2023-40-2023-50","text_gpt3_token_len":506,"char_repetition_ratio":0.122910075,"word_repetition_ratio":0.0,"special_character_ratio":0.23139377,"punctuation_ratio":0.11899313,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.976906,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-09T22:33:58Z\",\"WARC-Record-ID\":\"<urn:uuid:9f27eb6c-4db0-4b12-9717-acfe5005c1c6>\",\"Content-Length\":\"76587\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1df98cf3-6e1f-4a62-8bb2-263f76e57745>\",\"WARC-Concurrent-To\":\"<urn:uuid:6026a3c0-b584-45af-90e5-42ba141a00b4>\",\"WARC-IP-Address\":\"35.212.86.235\",\"WARC-Target-URI\":\"https://ericonanalytics.com/r-manipulation-data-table-vs-dplyr/\",\"WARC-Payload-Digest\":\"sha1:6RGFMOGNMNND5CJZVJ3ATMLBC3QF7OS7\",\"WARC-Block-Digest\":\"sha1:657QRSYANPGFLR25MPTP5INN67773MSU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100972.58_warc_CC-MAIN-20231209202131-20231209232131-00593.warc.gz\"}"}