start.Rmd 1.63 KB
Newer Older
Virgile Baudrot's avatar
Virgile Baudrot committed
1
---
Virgile Baudrot's avatar
Virgile Baudrot committed
2
title: "Getting started"
Virgile Baudrot's avatar
Virgile Baudrot committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{start}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```


Virgile Baudrot's avatar
Virgile Baudrot committed
20
The package `fdf` allows to work with column-list. When for instance you have a list of time series that you want to include in a `data.frame`.
Virgile Baudrot's avatar
Virgile Baudrot committed
21
22
23
24
25
26
27

Since it works only with `data.frame` object, there is no new object, you can use very easily with any package like `dplyr` (and the tidyverse) and spatio-temporal object with package `sf` for instance.

```{r setup}
library(fdf)
```

Virgile Baudrot's avatar
Virgile Baudrot committed
28
### Add a new column-list with `fd_add`
Virgile Baudrot's avatar
Virgile Baudrot committed
29
30

```{r addData}
Virgile Baudrot's avatar
Virgile Baudrot committed
31
DF = data.frame( A = 1:5)
Virgile Baudrot's avatar
Virgile Baudrot committed
32

Virgile Baudrot's avatar
Virgile Baudrot committed
33
DFD = fd_add(DF, "B", function(i) round(rnorm(runif(1, 4,10))))
Virgile Baudrot's avatar
Virgile Baudrot committed
34
35
36
DFD
```

Virgile Baudrot's avatar
Virgile Baudrot committed
37
### Filter element in a column-list with `fd_filter`
Virgile Baudrot's avatar
Virgile Baudrot committed
38
39
40
41
42
43

```{r filter}

fd_filter(DFD, "B", 3)
```

Virgile Baudrot's avatar
Virgile Baudrot committed
44
### Find index of element in column-list matching with a key using `fd_match`
Virgile Baudrot's avatar
Virgile Baudrot committed
45

Virgile Baudrot's avatar
Virgile Baudrot committed
46
```{r match}
Virgile Baudrot's avatar
Virgile Baudrot committed
47
# add new column
Virgile Baudrot's avatar
Virgile Baudrot committed
48
DFD = fd_add(DFD, "C", function(i) sample(1:5, 5))
Virgile Baudrot's avatar
Virgile Baudrot committed
49
DFD
Virgile Baudrot's avatar
Virgile Baudrot committed
50

Virgile Baudrot's avatar
Virgile Baudrot committed
51
# apply 'fd_match'
Virgile Baudrot's avatar
Virgile Baudrot committed
52
53
54
idMatch = fd_match(DFD, "C", 4)
idMatch

Virgile Baudrot's avatar
Virgile Baudrot committed
55
# check with 'fd_filter'
Virgile Baudrot's avatar
Virgile Baudrot committed
56
57
58
fd_filter(DFD, "C", idMatch)
```

Virgile Baudrot's avatar
Virgile Baudrot committed
59
### Melting column-list to a classical data.frame with `fd_melt`
Virgile Baudrot's avatar
Virgile Baudrot committed
60
61
62
63
64

```{r meltSimple}
fd_melt(DFD, "B", keep = list("A", "C"))
```

Virgile Baudrot's avatar
Virgile Baudrot committed
65
when two column-lists have the same length for each elements, we can melt in the same time
Virgile Baudrot's avatar
Virgile Baudrot committed
66
67

```{r meltDouble}
Virgile Baudrot's avatar
Virgile Baudrot committed
68
# add new column with element of same length between 'B' and 'B2'
Virgile Baudrot's avatar
Virgile Baudrot committed
69
DFD = fd_add(DFD, "B2", function(i) 1:length(DFD$B[[i]]))
Virgile Baudrot's avatar
Virgile Baudrot committed
70
DFD
Virgile Baudrot's avatar
Virgile Baudrot committed
71

Virgile Baudrot's avatar
Virgile Baudrot committed
72
# apply 'fd_melt'
Virgile Baudrot's avatar
Virgile Baudrot committed
73
74
fd_melt(DFD, "B", "B2", keep = list("A", "C"))
```