intro_r/doc/datatable_querying.org

68 lines
1.6 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
** Querying data.table
Sintaxis:
* ~DT[i, j, by]~
Subseting by "i" and "j"
** Subset rows in "i"
#+begin_src R
dt1 <- flights[origin == "JFK" & month == 6L]
dt1
#+end_src
Get the first two rows
#+begin_src R
flights[1:2]
#+end_src
#+RESULTS:
: year month day dep_delay arr_delay carrier origin dest air_time distance
: 1: 2014 1 1 14 13 AA JFK LAX 359 2475
: 2: 2014 1 1 -3 13 AA JFK LAX 363 2475
: hour
: 1: 9
: 2: 11
Ordering results of querying
#+begin_src R
dt_filt <- flights[order(origin, -dest)]
dt_filt
#+end_src