109 lines
2.4 KiB
Org Mode
109 lines
2.4 KiB
Org Mode
|
#+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: <style> #content{max-width:1800px;}</style>
|
||
|
#+HTML_HEAD: <style> p{max-width:800px;}</style>
|
||
|
#+HTML_HEAD: <style> li{max-width:800px;}</style>
|
||
|
#+HTML_HEAD: <style> pre.src{background: #f5f5f5;color:black;}</style>
|
||
|
#+HTML_HEAD: <style> .example{background: #fefefa;color:black;}</style>
|
||
|
|
||
|
|
||
|
#+begin_export html
|
||
|
<div id="subtitle" style="float: center; text-align: center;">
|
||
|
<p>
|
||
|
Org document with
|
||
|
<a href="http://www.r-project.org/">R</a> code
|
||
|
</p>
|
||
|
<p>
|
||
|
<a href="http://www.r-project.org/">
|
||
|
<img src="http://www.r-project.org/Rlogo.jpg"/>
|
||
|
</a>
|
||
|
</p>
|
||
|
</div>
|
||
|
#+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
|