-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_README.Rmd
100 lines (73 loc) · 2.31 KB
/
_README.Rmd
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
---
title: "SlideR: An R package for Sliding Windows"
author: "Joeri Jongbloets"
date: "14 April 2016"
output:
md_document:
variant: markdown_github
---
```{r global_options, include=FALSE}
require(knitr)
rm(list=ls())
knitr::opts_chunk$set(
##########################
eval = FALSE, # ENABLE OR DISABLE ALL CHUNK EVALUATION
##########################
echo = TRUE
)
```
[![Travis-CI Build Status](https://travis-ci.org/jjongbloets/slideR.svg?branch=master)](https://travis-ci.org/jjongbloets/slideR)
# SlideR
SlideR: An R package for Sliding Windows using dplyr, purrr and tidyr.
The package creates a nested data.frame with the columns; key, w_size and data.
## Installation
The package is in development and can be downloaded with `devtools`:
```{r}
# install.packages("devtools")
devtools::install_git("https://www.gitlab.com/mmp-uva/slider.git")
```
### Requirements
The package requires the following packages:
* `dplyr` (`install.packages("tidyverse")`)
* `purrr` (`install.packages("purrr")`)
* `tidyr` (`install.packages("tidtyr")`)
You can also install all these packages (and more) with one package:
* `tidyverse` (`install.packages("tidyverse")`)
## Usage
To load the package:
```{r}
library(slider)
```
### Partition data using a sliding window
```{r}
x <- 1:100
slide_window(x, w_size=10)
```
When using data.frames, a key needs to specified.
The key defines which column is used as key to the different windows.
```{r}
df <- data.frame(x = 1:100, y=c("a", "b"))
slide_window(df, key="x", w_size=10)
```
### Partition data using multiple sliding windows
It is often useful to apply multiple sliding windows to the same data,
while varying the size of the window.
This can be achieved using `slide_windows`
```{r}
x <- 1:100
slide_windows(x, w_sizes=c(5, 10, 15))
```
### Apply a function to each window
In most cases the sliding window is used to apply a function to segments of the data.
The function `apply_slide_window` and `apply_slide_windows` provide this functionality:
```{r}
x <- 1:100
apply_slide_window(x, mean, w_size=10)
# or
apply_slide_windows(x, mean, w_sizes=c(5, 10, 15))
```
For data.frames it is a bit more complicated, as one needs to specify the column name within the window.
```{r}
df <- data.frame(x = 1:100, y=c("a", "b"))
apply_slide_windows(df, ~mean(.$x), key="x", w_sizes=10)
```