-
Notifications
You must be signed in to change notification settings - Fork 0
/
tema6.qmd
65 lines (39 loc) · 1.62 KB
/
tema6.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
---
title: "Cadenas de operaciones"
---
Una función muy importante del paquete es la capacidad de encadenar operaciones en un mismo comando sin necesidad de guardar el resultado en algun objeto.
Es una función similar al uso del pipe `%>%` o `|>` en tidyverse o rbase. En data table se pueden evaluar una serie de expresiones una después de la otra escribiendolas entre corchetes de esta manera:
```
DT[ ... ][ ... ][ ... ]
```
o de forma vertical
```
DT[ ...
][ ...
][ ...
]
```
Por ejemplo, recapitulando temas anteriores, utilizando la tabla de registros de plantas de México podemos:
1. Seleccionar los registros del género Quercus y filtrar aquellos que no tienen información acerca de la especie o del año de colecta
```{.r}
library(data.table)
data = fread("data/plantae_mexico_conCoords_specimen.csv")
data[genus == "Quercus" & !is.na(species) & species != "" & !is.na(year)]
```
3. Agrupar por especie y por año de colecta
4. Contar el número de registros de que se realizó para cada especie en cada año
```{.r}
data[genus == "Quercus" & !is.na(species) & species != "" & !is.na(year), .(.N), by = .(year,species)]
```
5. Ordenar la columna de número de registros de mayor a menor utilizando una **cadena de operaciones**
```{.r}
data[genus == "Quercus" & !is.na(species) & species != "" & !is.na(year), .(N = .N), by = .(year,species)][order(year,-N)]
```
```{r}
#| message: false
#| warning: false
#| echo: false
library(data.table)
data = fread("data/data_quercus.csv")
data[genus == "Quercus" & !is.na(species) & species != "" & !is.na(year), .(.N), by = .(year,species)][order(year,-N)]
```