# library(installr)    # 在RGui中输入这两行命令,更新R
# updateR()

rm(list = ls())

Sys.setenv(LANGUAGE = "en_US.UTF-8")

library(rio)

df = import("flights14.csv")

export(df, "df.csv")

df_csv = import("df.csv")

export(df, "df.txt")

df_txt = import("df.txt")

export(df, "df.rds")

df_rds = import("df.rds")

export(df, "df.feather")
## Loading required namespace: feather
df_feather = import("df.feather")

export(df, "df.dta")

df_dta = import("df.dta")
rm(list = ls())

library(rio)

df = import("df.rds")

class(df)
## [1] "data.frame"
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
tb = as_tibble(df)

class(tb)
## [1] "tbl_df"     "tbl"        "data.frame"
library(data.table)
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## The following object is masked from 'package:purrr':
## 
##     transpose
dt = as.data.table(df)

class(dt)
## [1] "data.table" "data.frame"
dt_csv = fread("df.csv")

dt_txt = fread("df.txt")

library(dtplyr)

dt2tidy = lazy_dt(dt)

class(dt2tidy)
## [1] "dtplyr_step_first" "dtplyr_step"
dt2tidy
## Source: local data table [253,316 x 11]
## Call:   `_DT1`
## 
##    year month   day dep_delay arr_delay carrier origin dest  air_time distance
##   <int> <int> <int>     <int>     <int> <chr>   <chr>  <chr>    <int>    <int>
## 1  2014     1     1        14        13 AA      JFK    LAX        359     2475
## 2  2014     1     1        -3        13 AA      JFK    LAX        363     2475
## 3  2014     1     1         2         9 AA      JFK    LAX        351     2475
## 4  2014     1     1        -8       -26 AA      LGA    PBI        157     1035
## 5  2014     1     1         2         1 AA      JFK    LAX        350     2475
## 6  2014     1     1         4         0 AA      EWR    LAX        339     2454
## # ... with 253,310 more rows, and 1 more variable: hour <int>
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
rm(list = ls())

library(data.table)

# Econometric Analysis, 7th and 8th Edtion, Data Sets
# https://pages.stern.nyu.edu/~wgreene/Text/Edition7/tablelist8new.htm
# http://people.stern.nyu.edu/wgreene/Text/Edition7/

url1 = "http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF4-1.txt"

dt_url1 = fread(url1)

url2 = "http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF5-2.txt"

dt_url2 = fread(url2)

url3 = "http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF6-1.txt"

dt_url3 = fread(url3)

url4 = "http://www.wiley.com/legacy/wileychi/baltagi/supp/Gasoline.dat"

dt_url4 = fread(url4)

url5 = "http://www.wiley.com/legacy/wileychi/baltagi/supp/PRODUC.prn"

dt_url5 = fread(url5)
## Warning in fread(url5): Stopped early on line 819. Expected 11 fields but
## found 1. Consider fill=TRUE and comment.char=. First discarded non-empty line:
## <<PRODUC.PRN>>
url6 = "http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF10-4.txt"

dt_url6 = fread(url6)
## Warning in fread(url6): Detected 1 column names but the data has 5 columns (i.e.
## invalid file). Added 4 extra default column names at the end.
url7 = "http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF19-1.txt"

dt_url7 = fread(url7)

url8 = "http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF21-1.txt"

dt_url8 = fread(url8)
rm(list = ls())

df = mtcars %>% rownames_to_column(var = "car")

library(tidyverse)

tb = as_tibble(df)

library(data.table)

dt = as.data.table(df)

library(dtplyr)

dt2tb = lazy_dt(df)  # 使用dtplyr包时,将目标数据集惰性处理
# lazy_data可以通过as.data.frame();as.data.table();as_tibble()函数转换成相应形式的数据框

library(tidyverse)

dt2tb %>% 
  filter(vs == 1, wt > 3)  # 筛选出vs = 1,同时wt > 3的数据
## Source: local data table [6 x 12]
## Call:   `_DT2`[vs == 1 & wt > 3]
## 
##   car            mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Hornet 4 Dr~  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
## 2 Valiant       18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
## 3 Merc 240D     24.4     4  147.    62  3.69  3.19  20       1     0     4     2
## 4 Merc 230      22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
## 5 Merc 280      19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
## 6 Merc 280C     17.8     6  168.   123  3.92  3.44  18.9     1     0     4     4
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt2tb %>% 
  filter(row_number() == 1L:16L)  # 筛选data中前16行
## Source: local data table [16 x 12]
## Call:   `_DT2`[seq_len(.N) == 1L:16L]
## 
##   car            mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Mazda RX4     21       6   160   110  3.9   2.62  16.5     0     1     4     4
## 2 Mazda RX4 W~  21       6   160   110  3.9   2.88  17.0     0     1     4     4
## 3 Datsun 710    22.8     4   108    93  3.85  2.32  18.6     1     1     4     1
## 4 Hornet 4 Dr~  21.4     6   258   110  3.08  3.22  19.4     1     0     3     1
## 5 Hornet Spor~  18.7     8   360   175  3.15  3.44  17.0     0     0     3     2
## 6 Valiant       18.1     6   225   105  2.76  3.46  20.2     1     0     3     1
## # ... with 10 more rows
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt2tb %>% 
  arrange(hp, desc(drat))  # 以hp升序、drat降序排序
## Source: local data table [32 x 12]
## Call:   `_DT2`[order(hp, -drat)]
## 
##   car            mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Honda Civic   30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2
## 2 Merc 240D     24.4     4 147.     62  3.69  3.19  20       1     0     4     2
## 3 Toyota Coro~  33.9     4  71.1    65  4.22  1.84  19.9     1     1     4     1
## 4 Fiat 128      32.4     4  78.7    66  4.08  2.2   19.5     1     1     4     1
## 5 Fiat X1-9     27.3     4  79      66  4.08  1.94  18.9     1     1     4     1
## 6 Porsche 914~  26       4 120.     91  4.43  2.14  16.7     0     1     5     2
## # ... with 26 more rows
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt2tb %>% 
  select(cyl, disp)  # 提取cyl和disp列
## Source: local data table [32 x 2]
## Call:   `_DT2`[, .(cyl, disp)]
## 
##     cyl  disp
##   <dbl> <dbl>
## 1     6   160
## 2     6   160
## 3     4   108
## 4     6   258
## 5     8   360
## 6     6   225
## # ... with 26 more rows
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt2tb %>% 
  select(-mpg)  # 删除mpg列
## Source: local data table [32 x 11]
## Call:   `_DT2`[, .(car, cyl, disp, hp, drat, wt, qsec, vs, am, gear, 
##     carb)]
## 
##   car                 cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <chr>             <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Mazda RX4             6   160   110  3.9   2.62  16.5     0     1     4     4
## 2 Mazda RX4 Wag         6   160   110  3.9   2.88  17.0     0     1     4     4
## 3 Datsun 710            4   108    93  3.85  2.32  18.6     1     1     4     1
## 4 Hornet 4 Drive        6   258   110  3.08  3.22  19.4     1     0     3     1
## 5 Hornet Sportabout     8   360   175  3.15  3.44  17.0     0     0     3     2
## 6 Valiant               6   225   105  2.76  3.46  20.2     1     0     3     1
## # ... with 26 more rows
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt2tb %>% 
  select(vs, everything())  # 调整vs列至第一列
## Source: local data table [32 x 12]
## Call:   `_DT2`[, .(vs, car, mpg, cyl, disp, hp, drat, wt, qsec, am, gear, 
##     carb)]
## 
##      vs car            mpg   cyl  disp    hp  drat    wt  qsec    am  gear  carb
##   <dbl> <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1     0 Mazda RX4     21       6   160   110  3.9   2.62  16.5     1     4     4
## 2     0 Mazda RX4 W~  21       6   160   110  3.9   2.88  17.0     1     4     4
## 3     1 Datsun 710    22.8     4   108    93  3.85  2.32  18.6     1     4     1
## 4     1 Hornet 4 Dr~  21.4     6   258   110  3.08  3.22  19.4     0     3     1
## 5     0 Hornet Spor~  18.7     8   360   175  3.15  3.44  17.0     0     3     2
## 6     1 Valiant       18.1     6   225   105  2.76  3.46  20.2     0     3     1
## # ... with 26 more rows
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt2tb %>% 
  mutate(wt_drat = wt * drat) %>%  # 计算wt与drat的积添加到列wt_drat
  mutate(type = "old")  # 增加新列type赋值"old"
## Source: local data table [32 x 14]
## Call:   copy(`_DT2`)[, `:=`(wt_drat = wt * drat)][, `:=`(type = "old")]
## 
##   car            mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Mazda RX4     21       6   160   110  3.9   2.62  16.5     0     1     4     4
## 2 Mazda RX4 W~  21       6   160   110  3.9   2.88  17.0     0     1     4     4
## 3 Datsun 710    22.8     4   108    93  3.85  2.32  18.6     1     1     4     1
## 4 Hornet 4 Dr~  21.4     6   258   110  3.08  3.22  19.4     1     0     3     1
## 5 Hornet Spor~  18.7     8   360   175  3.15  3.44  17.0     0     0     3     2
## 6 Valiant       18.1     6   225   105  2.76  3.46  20.2     1     0     3     1
## # ... with 26 more rows, and 2 more variables: wt_drat <dbl>, type <chr>
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt2tb %>% 
  distinct(cyl, .keep_all = T)  # 以变量cyl去重,保留所有变量
## Source: local data table [3 x 12]
## Call:   unique(`_DT2`, by = "cyl")
## 
##   car            mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Mazda RX4     21       6   160   110  3.9   2.62  16.5     0     1     4     4
## 2 Datsun 710    22.8     4   108    93  3.85  2.32  18.6     1     1     4     1
## 3 Hornet Spor~  18.7     8   360   175  3.15  3.44  17.0     0     0     3     2
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt2tb %>%
  group_by(cyl) %>% 
  summarize(disp_mean = mean(disp))  # 根据cyl分组,计算disp均值
## Source: local data table [3 x 2]
## Call:   `_DT2`[, .(disp_mean = mean(disp)), keyby = .(cyl)]
## 
##     cyl disp_mean
##   <dbl>     <dbl>
## 1     4      105.
## 2     6      183.
## 3     8      353.
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
# 给data增加标签列id按序排号,调整id列值至第一列,将数据集赋值为data1
dt2tb1 = dt2tb %>% 
  mutate(id = 1:nrow(dt2tb)) %>% 
  select(id, everything())

# 提取data1中17到32行数据赋值为data2
dt2tb2 = dt2tb1 %>% 
  filter(row_number() == 17L:32L)

# 合并数据仅保留匹配的记录 *data1 ∩ data2
inner_join(dt2tb2, dt2tb1, by = "id")
## Source: local data table [16 x 25]
## Call:   setnames(copy(`_DT2`)[, `:=`(id = 1:nrow(..dt2tb))][, .(id, car, 
##     mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb)][seq_len(.N) == 
##     17L:32L][copy(`_DT2`)[, `:=`(id = 1:nrow(..dt2tb))][, .(id, 
##     car, mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb)], 
##     on = .(id), nomatch = NULL, allow.cartesian = TRUE], c("car", 
## "mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", 
## "gear", "carb", "i.car", "i.mpg", "i.cyl", "i.disp", "i.hp", 
## "i.drat", "i.wt", "i.qsec", "i.vs", "i.am", "i.gear", "i.carb"
## ), c("car.x", "mpg.x", "cyl.x", "disp.x", "hp.x", "drat.x", "wt.x", 
## "qsec.x", "vs.x", "am.x", "gear.x", "carb.x", "car.y", "mpg.y", 
## "cyl.y", "disp.y", "hp.y", "drat.y", "wt.y", "qsec.y", "vs.y", 
## "am.y", "gear.y", "carb.y"))
## 
##      id car.x    mpg.x cyl.x disp.x  hp.x drat.x  wt.x qsec.x  vs.x  am.x gear.x
##   <int> <chr>    <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl> <dbl>  <dbl>
## 1    17 Chrysle~  14.7     8  440     230   3.23  5.34   17.4     0     0      3
## 2    18 Fiat 128  32.4     4   78.7    66   4.08  2.2    19.5     1     1      4
## 3    19 Honda C~  30.4     4   75.7    52   4.93  1.62   18.5     1     1      4
## 4    20 Toyota ~  33.9     4   71.1    65   4.22  1.84   19.9     1     1      4
## 5    21 Toyota ~  21.5     4  120.     97   3.7   2.46   20.0     1     0      3
## 6    22 Dodge C~  15.5     8  318     150   2.76  3.52   16.9     0     0      3
## # ... with 10 more rows, and 13 more variables: carb.x <dbl>, car.y <chr>,
## #   mpg.y <dbl>, cyl.y <dbl>, disp.y <dbl>, hp.y <dbl>, drat.y <dbl>,
## #   wt.y <dbl>, qsec.y <dbl>, vs.y <dbl>, am.y <dbl>, gear.y <dbl>,
## #   carb.y <dbl>
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
# 将data1,data2以data2中id列为基准进行匹配连接
left_join(dt2tb2, dt2tb1, by = "id")
## Source: local data table [16 x 25]
## Call:   setnames(setcolorder(copy(`_DT2`)[, `:=`(id = 1:nrow(..dt2tb))][, 
##     .(id, car, mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, 
##         carb)][copy(`_DT2`)[, `:=`(id = 1:nrow(..dt2tb))][, .(id, 
##     car, mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb)][seq_len(.N) == 
##     17L:32L], on = .(id), allow.cartesian = TRUE], c(1L, 14L, 
## 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 2L, 3L, 
## 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L)), c("i.car", "i.mpg", 
## "i.cyl", "i.disp", "i.hp", "i.drat", "i.wt", "i.qsec", "i.vs", 
## "i.am", "i.gear", "i.carb", "car", "mpg", "cyl", "disp", "hp", 
## "drat", "wt", "qsec", "vs", "am", "gear", "carb"), c("car.x", 
## "mpg.x", "cyl.x", "disp.x", "hp.x", "drat.x", "wt.x", "qsec.x", 
## "vs.x", "am.x", "gear.x", "carb.x", "car.y", "mpg.y", "cyl.y", 
## "disp.y", "hp.y", "drat.y", "wt.y", "qsec.y", "vs.y", "am.y", 
## "gear.y", "carb.y"))
## 
##      id car.x    mpg.x cyl.x disp.x  hp.x drat.x  wt.x qsec.x  vs.x  am.x gear.x
##   <int> <chr>    <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl> <dbl>  <dbl>
## 1    17 Chrysle~  14.7     8  440     230   3.23  5.34   17.4     0     0      3
## 2    18 Fiat 128  32.4     4   78.7    66   4.08  2.2    19.5     1     1      4
## 3    19 Honda C~  30.4     4   75.7    52   4.93  1.62   18.5     1     1      4
## 4    20 Toyota ~  33.9     4   71.1    65   4.22  1.84   19.9     1     1      4
## 5    21 Toyota ~  21.5     4  120.     97   3.7   2.46   20.0     1     0      3
## 6    22 Dodge C~  15.5     8  318     150   2.76  3.52   16.9     0     0      3
## # ... with 10 more rows, and 13 more variables: carb.x <dbl>, car.y <chr>,
## #   mpg.y <dbl>, cyl.y <dbl>, disp.y <dbl>, hp.y <dbl>, drat.y <dbl>,
## #   wt.y <dbl>, qsec.y <dbl>, vs.y <dbl>, am.y <dbl>, gear.y <dbl>,
## #   carb.y <dbl>
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
# 合并数据保留所有记录 *data1 ∪ data2
full_join(dt2tb1, dt2tb2, by = "id")
## Source: local data table [32 x 25]
## Call:   merge(copy(`_DT2`)[, `:=`(id = 1:nrow(..dt2tb))][, .(id, car, 
##     mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb)], 
##     copy(`_DT2`)[, `:=`(id = 1:nrow(..dt2tb))][, .(id, car, mpg, 
##         cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb)][seq_len(.N) == 
##         17L:32L], all = TRUE, by.x = "id", by.y = "id", allow.cartesian = TRUE)
## 
##      id car.x    mpg.x cyl.x disp.x  hp.x drat.x  wt.x qsec.x  vs.x  am.x gear.x
##   <int> <chr>    <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl> <dbl>  <dbl>
## 1     1 Mazda R~  21       6    160   110   3.9   2.62   16.5     0     1      4
## 2     2 Mazda R~  21       6    160   110   3.9   2.88   17.0     0     1      4
## 3     3 Datsun ~  22.8     4    108    93   3.85  2.32   18.6     1     1      4
## 4     4 Hornet ~  21.4     6    258   110   3.08  3.22   19.4     1     0      3
## 5     5 Hornet ~  18.7     8    360   175   3.15  3.44   17.0     0     0      3
## 6     6 Valiant   18.1     6    225   105   2.76  3.46   20.2     1     0      3
## # ... with 26 more rows, and 13 more variables: carb.x <dbl>, car.y <chr>,
## #   mpg.y <dbl>, cyl.y <dbl>, disp.y <dbl>, hp.y <dbl>, drat.y <dbl>,
## #   wt.y <dbl>, qsec.y <dbl>, vs.y <dbl>, am.y <dbl>, gear.y <dbl>,
## #   carb.y <dbl>
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
# 返回无法与data2匹配的data1的所有记录 *data1 - (data1 ∩ data2)
anti_join(dt2tb1, dt2tb2, by = "id")
## Source: local data table [16 x 13]
## Call:   copy(`_DT2`)[, `:=`(id = 1:nrow(..dt2tb))][, .(id, car, mpg, 
##     cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb)][!copy(`_DT2`)[, 
##     `:=`(id = 1:nrow(..dt2tb))][, .(id, car, mpg, cyl, disp, 
##     hp, drat, wt, qsec, vs, am, gear, carb)][seq_len(.N) == 17L:32L], 
##     on = .(id)]
## 
##      id car      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <int> <chr>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1     1 Mazda~  21       6   160   110  3.9   2.62  16.5     0     1     4     4
## 2     2 Mazda~  21       6   160   110  3.9   2.88  17.0     0     1     4     4
## 3     3 Datsu~  22.8     4   108    93  3.85  2.32  18.6     1     1     4     1
## 4     4 Horne~  21.4     6   258   110  3.08  3.22  19.4     1     0     3     1
## 5     5 Horne~  18.7     8   360   175  3.15  3.44  17.0     0     0     3     2
## 6     6 Valia~  18.1     6   225   105  2.76  3.46  20.2     1     0     3     1
## # ... with 10 more rows
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
# 返回能够与data2匹配的data1的所有记录
semi_join(dt2tb1, dt2tb2, by = "id")
## Source: local data table [16 x 13]
## Call:   copy(`_DT2`)[, `:=`(id = 1:nrow(..dt2tb))][, .(id, car, mpg, 
##     cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb)][unique(copy(`_DT2`)[, 
##     `:=`(id = 1:nrow(..dt2tb))][, .(id, car, mpg, cyl, disp, 
##     hp, drat, wt, qsec, vs, am, gear, carb)][copy(`_DT2`)[, `:=`(id = 1:nrow(..dt2tb))][, 
##     .(id, car, mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, 
##         carb)][seq_len(.N) == 17L:32L], which = TRUE, nomatch = NULL, 
##     on = .(id)])]
## 
##      id car      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <int> <chr>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1    17 Chrys~  14.7     8 440     230  3.23  5.34  17.4     0     0     3     4
## 2    18 Fiat ~  32.4     4  78.7    66  4.08  2.2   19.5     1     1     4     1
## 3    19 Honda~  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2
## 4    20 Toyot~  33.9     4  71.1    65  4.22  1.84  19.9     1     1     4     1
## 5    21 Toyot~  21.5     4 120.     97  3.7   2.46  20.0     1     0     3     1
## 6    22 Dodge~  15.5     8 318     150  2.76  3.52  16.9     0     0     3     2
## # ... with 10 more rows
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
# Selecting column(s)
dt2tb %>% select(mpg, hp)
## Source: local data table [32 x 2]
## Call:   `_DT2`[, .(mpg, hp)]
## 
##     mpg    hp
##   <dbl> <dbl>
## 1  21     110
## 2  21     110
## 3  22.8    93
## 4  21.4   110
## 5  18.7   175
## 6  18.1   105
## # ... with 26 more rows
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt[, .(mpg, hp)]
##      mpg  hp
##  1: 21.0 110
##  2: 21.0 110
##  3: 22.8  93
##  4: 21.4 110
##  5: 18.7 175
##  6: 18.1 105
##  7: 14.3 245
##  8: 24.4  62
##  9: 22.8  95
## 10: 19.2 123
## 11: 17.8 123
## 12: 16.4 180
## 13: 17.3 180
## 14: 15.2 180
## 15: 10.4 205
## 16: 10.4 215
## 17: 14.7 230
## 18: 32.4  66
## 19: 30.4  52
## 20: 33.9  65
## 21: 21.5  97
## 22: 15.5 150
## 23: 15.2 150
## 24: 13.3 245
## 25: 19.2 175
## 26: 27.3  66
## 27: 26.0  91
## 28: 30.4 113
## 29: 15.8 264
## 30: 19.7 175
## 31: 15.0 335
## 32: 21.4 109
##      mpg  hp
# Subsetting by row number
dt2tb %>% slice(1:3)
## Source: local data table [3 x 12]
## Call:   `_DT2`[(1:3)[between(1:3, -.N, .N)]]
## 
##   car            mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Mazda RX4     21       6   160   110  3.9   2.62  16.5     0     1     4     4
## 2 Mazda RX4 W~  21       6   160   110  3.9   2.88  17.0     0     1     4     4
## 3 Datsun 710    22.8     4   108    93  3.85  2.32  18.6     1     1     4     1
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt[1:3, , ]
##              car  mpg cyl disp  hp drat    wt  qsec vs am gear carb
## 1:     Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## 2: Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## 3:    Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
# Subsetting by variable values
dt2tb %>% filter(mpg > 20 )
## Source: local data table [14 x 12]
## Call:   `_DT2`[mpg > 20]
## 
##   car            mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Mazda RX4     21       6  160    110  3.9   2.62  16.5     0     1     4     4
## 2 Mazda RX4 W~  21       6  160    110  3.9   2.88  17.0     0     1     4     4
## 3 Datsun 710    22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
## 4 Hornet 4 Dr~  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
## 5 Merc 240D     24.4     4  147.    62  3.69  3.19  20       1     0     4     2
## 6 Merc 230      22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
## # ... with 8 more rows
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt[mpg > 20, ]
##                car  mpg cyl  disp  hp drat    wt  qsec vs am gear carb
##  1:      Mazda RX4 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
##  2:  Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
##  3:     Datsun 710 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
##  4: Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
##  5:      Merc 240D 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
##  6:       Merc 230 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
##  7:       Fiat 128 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
##  8:    Honda Civic 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
##  9: Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## 10:  Toyota Corona 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## 11:      Fiat X1-9 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## 12:  Porsche 914-2 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## 13:   Lotus Europa 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## 14:     Volvo 142E 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
# Sorting a table (in ascending order)
dt2tb %>% arrange(mpg)
## Source: local data table [32 x 12]
## Call:   `_DT2`[order(mpg)]
## 
##   car            mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Cadillac Fl~  10.4     8   472   205  2.93  5.25  18.0     0     0     3     4
## 2 Lincoln Con~  10.4     8   460   215  3     5.42  17.8     0     0     3     4
## 3 Camaro Z28    13.3     8   350   245  3.73  3.84  15.4     0     0     3     4
## 4 Duster 360    14.3     8   360   245  3.21  3.57  15.8     0     0     3     4
## 5 Chrysler Im~  14.7     8   440   230  3.23  5.34  17.4     0     0     3     4
## 6 Maserati Bo~  15       8   301   335  3.54  3.57  14.6     0     1     5     8
## # ... with 26 more rows
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt[order(mpg), ]
##                     car  mpg cyl  disp  hp drat    wt  qsec vs am gear carb
##  1:  Cadillac Fleetwood 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
##  2: Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
##  3:          Camaro Z28 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
##  4:          Duster 360 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
##  5:   Chrysler Imperial 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
##  6:       Maserati Bora 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
##  7:         Merc 450SLC 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
##  8:         AMC Javelin 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
##  9:    Dodge Challenger 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## 10:      Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## 11:          Merc 450SE 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## 12:          Merc 450SL 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## 13:           Merc 280C 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## 14:             Valiant 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## 15:   Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## 16:            Merc 280 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## 17:    Pontiac Firebird 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## 18:        Ferrari Dino 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## 19:           Mazda RX4 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## 20:       Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## 21:      Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## 22:          Volvo 142E 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
## 23:       Toyota Corona 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## 24:          Datsun 710 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## 25:            Merc 230 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## 26:           Merc 240D 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## 27:       Porsche 914-2 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## 28:           Fiat X1-9 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## 29:         Honda Civic 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## 30:        Lotus Europa 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## 31:            Fiat 128 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## 32:      Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
##                     car  mpg cyl  disp  hp drat    wt  qsec vs am gear carb
# Sorting a table (in descending order)
dt2tb %>% arrange(- mpg)
## Source: local data table [32 x 12]
## Call:   `_DT2`[order(-mpg)]
## 
##   car            mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Toyota Coro~  33.9     4  71.1    65  4.22  1.84  19.9     1     1     4     1
## 2 Fiat 128      32.4     4  78.7    66  4.08  2.2   19.5     1     1     4     1
## 3 Honda Civic   30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2
## 4 Lotus Europa  30.4     4  95.1   113  3.77  1.51  16.9     1     1     5     2
## 5 Fiat X1-9     27.3     4  79      66  4.08  1.94  18.9     1     1     4     1
## 6 Porsche 914~  26       4 120.     91  4.43  2.14  16.7     0     1     5     2
## # ... with 26 more rows
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt[order(- mpg), ]
##                     car  mpg cyl  disp  hp drat    wt  qsec vs am gear carb
##  1:      Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
##  2:            Fiat 128 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
##  3:         Honda Civic 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
##  4:        Lotus Europa 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
##  5:           Fiat X1-9 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
##  6:       Porsche 914-2 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
##  7:           Merc 240D 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
##  8:          Datsun 710 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
##  9:            Merc 230 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## 10:       Toyota Corona 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## 11:      Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## 12:          Volvo 142E 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
## 13:           Mazda RX4 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## 14:       Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## 15:        Ferrari Dino 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## 16:            Merc 280 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## 17:    Pontiac Firebird 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## 18:   Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## 19:             Valiant 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## 20:           Merc 280C 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## 21:          Merc 450SL 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## 22:          Merc 450SE 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## 23:      Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## 24:    Dodge Challenger 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## 25:         Merc 450SLC 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## 26:         AMC Javelin 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## 27:       Maserati Bora 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## 28:   Chrysler Imperial 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## 29:          Duster 360 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## 30:          Camaro Z28 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## 31:  Cadillac Fleetwood 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## 32: Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
##                     car  mpg cyl  disp  hp drat    wt  qsec vs am gear carb
# Sorting by multiple columns
dt2tb %>% arrange(cyl, mpg)
## Source: local data table [32 x 12]
## Call:   `_DT2`[order(cyl, mpg)]
## 
##   car            mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Volvo 142E    21.4     4  121    109  4.11  2.78  18.6     1     1     4     2
## 2 Toyota Coro~  21.5     4  120.    97  3.7   2.46  20.0     1     0     3     1
## 3 Datsun 710    22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
## 4 Merc 230      22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
## 5 Merc 240D     24.4     4  147.    62  3.69  3.19  20       1     0     4     2
## 6 Porsche 914~  26       4  120.    91  4.43  2.14  16.7     0     1     5     2
## # ... with 26 more rows
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt[order(cyl, mpg), ]
##                     car  mpg cyl  disp  hp drat    wt  qsec vs am gear carb
##  1:          Volvo 142E 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
##  2:       Toyota Corona 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
##  3:          Datsun 710 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
##  4:            Merc 230 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
##  5:           Merc 240D 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
##  6:       Porsche 914-2 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
##  7:           Fiat X1-9 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
##  8:         Honda Civic 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
##  9:        Lotus Europa 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## 10:            Fiat 128 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## 11:      Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## 12:           Merc 280C 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## 13:             Valiant 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## 14:            Merc 280 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## 15:        Ferrari Dino 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## 16:           Mazda RX4 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## 17:       Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## 18:      Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## 19:  Cadillac Fleetwood 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## 20: Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## 21:          Camaro Z28 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## 22:          Duster 360 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## 23:   Chrysler Imperial 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## 24:       Maserati Bora 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## 25:         Merc 450SLC 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## 26:         AMC Javelin 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## 27:    Dodge Challenger 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## 28:      Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## 29:          Merc 450SE 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## 30:          Merc 450SL 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## 31:   Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## 32:    Pontiac Firebird 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
##                     car  mpg cyl  disp  hp drat    wt  qsec vs am gear carb
# Creating a new column
dt2tb3 = dt2tb %>% mutate(gpm = 1 / mpg)  # %>% as_tibble()

dt[, gpm := 1 / mpg, ]

# Deleting a column
dt2tb4 = dt2tb3 %>% select(- gpm)

dt[, gpm := NULL, ]

# Summarizing specific columns
dt2tb %>% summarize(mpg = median(mpg), hp = mean(hp))
## Source: local data table [1 x 2]
## Call:   `_DT2`[, .(mpg = median(mpg), hp = mean(hp))]
## 
##     mpg    hp
##   <dbl> <dbl>
## 1  19.2  147.
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt[, .(mpg = median(mpg), hp = mean(hp)), ]
##     mpg       hp
## 1: 19.2 146.6875
# Summarizing all columns
dt2tb %>% summarize(across(everything(), max))
## Source: local data table [1 x 12]
## Call:   `_DT2`[, .(car = max(car), mpg = max(mpg), cyl = max(cyl), disp = max(disp), 
##     hp = max(hp), drat = max(drat), wt = max(wt), qsec = max(qsec), 
##     vs = max(vs), am = max(am), gear = max(gear), carb = max(carb))]
## 
##   car          mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <chr>      <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Volvo 142E  33.9     8   472   335  4.93  5.42  22.9     1     1     5     8
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt[, lapply(.SD, max), ]
##           car  mpg cyl disp  hp drat    wt qsec vs am gear carb
## 1: Volvo 142E 33.9   8  472 335 4.93 5.424 22.9  1  1    5    8
# Summarizing all columns by group
dt2tb %>% group_by(cyl) %>% summarize(across(everything(), max))
## Source: local data table [3 x 12]
## Call:   `_DT2`[, .(car = max(car), mpg = max(mpg), disp = max(disp), 
##     hp = max(hp), drat = max(drat), wt = max(wt), qsec = max(qsec), 
##     vs = max(vs), am = max(am), gear = max(gear), carb = max(carb)), 
##     keyby = .(cyl)]
## 
##     cyl car            mpg  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <dbl> <chr>        <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1     4 Volvo 142E    33.9  147.   113  4.93  3.19  22.9     1     1     5     2
## 2     6 Valiant       21.4  258    175  3.92  3.46  20.2     1     1     5     6
## 3     8 Pontiac Fir~  19.2  472    335  4.22  5.42  18       0     1     5     8
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt[, lapply(.SD, max), by = cyl]
##    cyl              car  mpg  disp  hp drat    wt  qsec vs am gear carb
## 1:   6          Valiant 21.4 258.0 175 3.92 3.460 20.22  1  1    5    6
## 2:   4       Volvo 142E 33.9 146.7 113 4.93 3.190 22.90  1  1    5    2
## 3:   8 Pontiac Firebird 19.2 472.0 335 4.22 5.424 18.00  0  1    5    8
# Pivoting table to long format
dt2tb_L = dt2tb %>% 
  pivot_longer(cols = - car, names_to = "variable", values_to = "value") %>% 
  as_tibble()

dt.L = melt(dt, id.vars = c("car"), variable.name = "variable", value.name = "value")

# Pivoting table to wide format
dt2tb_W = dt2tb_L %>% 
  pivot_wider(names_from = "variable", values_from = "value") %>% 
  as_tibble()

dt.W = dcast(dt.L, car ~ variable, value.var = "value")

# Joining tables
left_join(dt2tb2, dt2tb1, by = "id")
## Source: local data table [16 x 25]
## Call:   setnames(setcolorder(copy(`_DT2`)[, `:=`(id = 1:nrow(..dt2tb))][, 
##     .(id, car, mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, 
##         carb)][copy(`_DT2`)[, `:=`(id = 1:nrow(..dt2tb))][, .(id, 
##     car, mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb)][seq_len(.N) == 
##     17L:32L], on = .(id), allow.cartesian = TRUE], c(1L, 14L, 
## 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 2L, 3L, 
## 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L)), c("i.car", "i.mpg", 
## "i.cyl", "i.disp", "i.hp", "i.drat", "i.wt", "i.qsec", "i.vs", 
## "i.am", "i.gear", "i.carb", "car", "mpg", "cyl", "disp", "hp", 
## "drat", "wt", "qsec", "vs", "am", "gear", "carb"), c("car.x", 
## "mpg.x", "cyl.x", "disp.x", "hp.x", "drat.x", "wt.x", "qsec.x", 
## "vs.x", "am.x", "gear.x", "carb.x", "car.y", "mpg.y", "cyl.y", 
## "disp.y", "hp.y", "drat.y", "wt.y", "qsec.y", "vs.y", "am.y", 
## "gear.y", "carb.y"))
## 
##      id car.x    mpg.x cyl.x disp.x  hp.x drat.x  wt.x qsec.x  vs.x  am.x gear.x
##   <int> <chr>    <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl>  <dbl> <dbl> <dbl>  <dbl>
## 1    17 Chrysle~  14.7     8  440     230   3.23  5.34   17.4     0     0      3
## 2    18 Fiat 128  32.4     4   78.7    66   4.08  2.2    19.5     1     1      4
## 3    19 Honda C~  30.4     4   75.7    52   4.93  1.62   18.5     1     1      4
## 4    20 Toyota ~  33.9     4   71.1    65   4.22  1.84   19.9     1     1      4
## 5    21 Toyota ~  21.5     4  120.     97   3.7   2.46   20.0     1     0      3
## 6    22 Dodge C~  15.5     8  318     150   2.76  3.52   16.9     0     0      3
## # ... with 10 more rows, and 13 more variables: carb.x <dbl>, car.y <chr>,
## #   mpg.y <dbl>, cyl.y <dbl>, disp.y <dbl>, hp.y <dbl>, drat.y <dbl>,
## #   wt.y <dbl>, qsec.y <dbl>, vs.y <dbl>, am.y <dbl>, gear.y <dbl>,
## #   carb.y <dbl>
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
#dt2tb2[dt2tb1, on = "id"]

# Chaining commands
dt2tb %>% 
  mutate(gpm = 1/mpg) %>% 
  group_by(cyl) %>% 
  summarize(gpm = median(gpm)) %>% 
  arrange(- gpm)
## Source: local data table [3 x 2]
## Call:   copy(`_DT2`)[, `:=`(gpm = 1/mpg)][, .(gpm = median(gpm)), keyby = .(cyl)][order(-gpm)]
## 
##     cyl    gpm
##   <dbl>  <dbl>
## 1     8 0.0658
## 2     6 0.0508
## 3     4 0.0385
## 
## # Use as.data.table()/as.data.frame()/as_tibble() to access results
dt[, gpm := 1/mpg, ][
  order(-gpm), .(gpm = median(gpm)), by = cyl]
##    cyl        gpm
## 1:   8 0.06578947
## 2:   6 0.05076142
## 3:   4 0.03846154