#+TITLE: Datatable package
#+DATE: 2021-10-26
#+OPTIONS: creator:nil timestamp:nil todo:nil num:nil
#+PROPERTY: header-args:R :results output :session *Rc* :cmdline :tangle yes
#+PROPERTY: header-args:R+ :exports both
#+SETUPFILE: https://fniessen.github.io/org-html-themes/org/theme-readtheorg.setup
#+HTML_HEAD:
#+HTML_HEAD:
#+HTML_HEAD:
#+HTML_HEAD:
#+HTML_HEAD:
#+begin_export html
Org document with
R code
#+end_export
#+begin_src R :exports code
rm(list = ls())
#+end_src
** Select columns in "j"
This creates a vector
#+begin_src R
dt_c <- flights[, arr_delay]
head(dt_c)
#+end_src
To create a data.table, use the function "~list~" or dot "~.~"
#+begin_src R
dt_c1 <- flights[, list(arr_delay)]
head(dt_c1)
#+end_src
The selection is wrapped in "~list~", thus, the columns can be
renamed
#+begin_src R
dt_c2 <- flights[, .(delay2arriv = arr_delay, dist = distance)]
head(dt_c2)
#+end_src
#+begin_src R
dt_c2a <- flights[, c("arr_delay", "distance")]
head(dt_c2a)
#+end_src
#+begin_src R
dt_c2b <- flights[, year:day]
head(dt_c2b)
#+end_src
Building new colummns
#+begin_src R
head(dt_c2b[, `:=`(time = year -1, # name = value,
pumpkin = month,
tomatoe = day +1)])
#+end_src
Renaming colummns
#+begin_src R
setnames(dt_c2b,
c("time", "pumpkin", "tomatoe"),# old names
c("time_minus1", "pumpkin_month", "day_plus1")) # new names
names(dt_c2b)
#+end_src
#+begin_src R
setnames(dt_c2b, toupper) # new names
names(dt_c2b)
#+end_src
Renaming colummns
#+begin_src R
setnames(dt_c2b, tolower) # new names
names(dt_c2b)
#+end_src
#+begin_src R
dt_c2c <- flights[, -(year:day)]
head(dt_c2c)
#+end_src
Select columns by patterns, e.g., selecting 5 lines form columns having "del"
characters in the name:
#+begin_src R
flights[1:5, names(flights) %like% "del", with = FALSE]
#+end_src