Skip to content

Commit c8d1e71

Browse files
committed
add a resuffled example to tq_sa, etc.
1 parent 7c27160 commit c8d1e71

File tree

2 files changed

+84
-26
lines changed

2 files changed

+84
-26
lines changed

README.Rmd

+21-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ This package is only for my own use for now. "You" means "future me".
5555

5656
You can install the development version with:
5757

58-
``` r
58+
```{r install, eval=FALSE}
59+
5960
remotes::install_github("mitsuoxv/tqr")
6061
6162
```
@@ -103,7 +104,7 @@ aus_production %>%
103104

104105
In the above examples, `sum` function transforms a numeric vector.
105106

106-
tsibble package provides `difference` function, which also transforms a time-wise numeric vector. Using it, I can create a new variable "diff".
107+
tsibble package provides `difference` function, which also transforms a time-wise numeric vector. Using it, you can create a new variable "diff".
107108

108109
```{r difference1}
109110
@@ -114,7 +115,7 @@ aus_arrivals %>%
114115
115116
```
116117

117-
Or I can transform a existing variable "Arrivals". I can use `difference` function flexibly.
118+
Or you can transform a existing variable "Arrivals". You can use `difference` function flexibly.
118119

119120
```{r difference2}
120121
@@ -192,8 +193,13 @@ scrambled %>%
192193
```{r moving_average}
193194
194195
aus_livestock %>%
196+
mutate(Count = if_else(row_number() == 4, NA_real_, Count)) %>%
195197
group_by_key() %>%
196-
mutate(ma3 = moving_average(Count, n = 3)) %>%
198+
mutate(
199+
ma3 = moving_average(Count, n = 3),
200+
ma3_na.rm = moving_average(Count, n = 3, na.rm = TRUE),
201+
ma3_left = moving_average(Count, n = 3, .align = "left")
202+
) %>%
197203
ungroup()
198204
199205
```
@@ -249,12 +255,22 @@ aus_arrivals %>%
249255

250256
`tq_sa` function transforms "Arrivals" variable in this example.
251257

252-
```{r tq_sa}
258+
```{r tq_sa1}
253259
254260
aus_arrivals %>%
255261
tq_sa()
256262
257263
```
258264

265+
`tq_sa` function returns right even if the rows are reshuffled, as `tq_diff`, `tq_ma` and `tq_gr` functions do.
266+
267+
```{r tq_sa2}
268+
269+
aus_arrivals %>%
270+
slice(sample(nrow(aus_arrivals))) %>%
271+
tq_sa() %>%
272+
arrange(Origin, Quarter)
273+
274+
```
259275

260276
EOL

README.md

+63-21
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ aus_production %>%
184184
In the above examples, `sum` function transforms a numeric vector.
185185

186186
tsibble package provides `difference` function, which also transforms a
187-
time-wise numeric vector. Using it, I can create a new variable “diff”.
187+
time-wise numeric vector. Using it, you can create a new variable
188+
“diff”.
188189

189190
``` r
190191
aus_arrivals %>%
@@ -208,7 +209,7 @@ aus_arrivals %>%
208209
#> # … with 498 more rows
209210
```
210211

211-
Or I can transform a existing variable “Arrivals”. I can use
212+
Or you can transform a existing variable “Arrivals”. You can use
212213
`difference` function flexibly.
213214

214215
``` r
@@ -300,11 +301,11 @@ arrange(wrong, year)
300301
#> # A tsibble: 6 x 3 [1Y]
301302
#> year value diff
302303
#> <int> <dbl> <dbl>
303-
#> 1 2000 0 NA
304-
#> 2 2001 1 -24
305-
#> 3 2002 4 4
306-
#> 4 2003 9 5
307-
#> 5 2004 16 15
304+
#> 1 2000 0 -4
305+
#> 2 2001 1 1
306+
#> 3 2002 4 -12
307+
#> 4 2003 9 8
308+
#> 5 2004 16 NA
308309
#> 6 2005 25 16
309310

310311
right <- mutate(scrambled, diff = difference(value, order_by = year))
@@ -364,23 +365,28 @@ refer to its document.
364365

365366
``` r
366367
aus_livestock %>%
368+
mutate(Count = if_else(row_number() == 4, NA_real_, Count)) %>%
367369
group_by_key() %>%
368-
mutate(ma3 = moving_average(Count, n = 3)) %>%
370+
mutate(
371+
ma3 = moving_average(Count, n = 3),
372+
ma3_na.rm = moving_average(Count, n = 3, na.rm = TRUE),
373+
ma3_left = moving_average(Count, n = 3, .align = "left")
374+
) %>%
369375
ungroup()
370-
#> # A tsibble: 29,364 x 5 [1M]
376+
#> # A tsibble: 29,364 x 7 [1M]
371377
#> # Key: Animal, State [54]
372-
#> Month Animal State Count ma3
373-
#> <mth> <fct> <fct> <dbl> <dbl>
374-
#> 1 1976 Jul Bulls, bullocks and steers Australian Capital Territory 2300 NA
375-
#> 2 1976 Aug Bulls, bullocks and steers Australian Capital Territory 2100 NA
376-
#> 3 1976 Sep Bulls, bullocks and steers Australian Capital Territory 2100 2167.
377-
#> 4 1976 Oct Bulls, bullocks and steers Australian Capital Territory 1900 2033.
378-
#> 5 1976 Nov Bulls, bullocks and steers Australian Capital Territory 2100 2033.
379-
#> 6 1976 Dec Bulls, bullocks and steers Australian Capital Territory 1800 1933.
380-
#> 7 1977 Jan Bulls, bullocks and steers Australian Capital Territory 1800 1900
381-
#> 8 1977 Feb Bulls, bullocks and steers Australian Capital Territory 1900 1833.
382-
#> 9 1977 Mar Bulls, bullocks and steers Australian Capital Territory 2700 2133.
383-
#> 10 1977 Apr Bulls, bullocks and steers Australian Capital Territory 2300 2300
378+
#> Month Animal State Count ma3 ma3_na.rm ma3_left
379+
#> <mth> <fct> <fct> <dbl> <dbl> <dbl> <dbl>
380+
#> 1 1976 Jul Bulls, bullocks and steers Australia… 2300 NA NA 2167.
381+
#> 2 1976 Aug Bulls, bullocks and steers Australia… 2100 NA NA NA
382+
#> 3 1976 Sep Bulls, bullocks and steers Australia… 2100 2167. 2167. NA
383+
#> 4 1976 Oct Bulls, bullocks and steers Australia… NA NA 2100 NA
384+
#> 5 1976 Nov Bulls, bullocks and steers Australia… 2100 NA 2100 1900
385+
#> 6 1976 Dec Bulls, bullocks and steers Australia… 1800 NA 1950 1833.
386+
#> 7 1977 Jan Bulls, bullocks and steers Australia… 1800 1900 1900 2133.
387+
#> 8 1977 Feb Bulls, bullocks and steers Australia… 1900 1833. 1833. 2300
388+
#> 9 1977 Mar Bulls, bullocks and steers Australia… 2700 2133. 2133. 2500
389+
#> 10 1977 Apr Bulls, bullocks and steers Australia… 2300 2300 2300 2567.
384390
#> # … with 29,354 more rows
385391
```
386392

@@ -514,4 +520,40 @@ aus_arrivals %>%
514520
#> # … with 498 more rows
515521
```
516522

523+
`tq_sa` function returns right even if the rows are reshuffled, as
524+
`tq_diff`, `tq_ma` and `tq_gr` functions do.
525+
526+
``` r
527+
aus_arrivals %>%
528+
slice(sample(nrow(aus_arrivals))) %>%
529+
tq_sa() %>%
530+
arrange(Origin, Quarter)
531+
#> Warning: Current temporal ordering may yield unexpected results.
532+
#> ℹ Suggest to sort by `Origin`, `Quarter` first.
533+
534+
#> Warning: Current temporal ordering may yield unexpected results.
535+
#> ℹ Suggest to sort by `Origin`, `Quarter` first.
536+
537+
#> Warning: Current temporal ordering may yield unexpected results.
538+
#> ℹ Suggest to sort by `Origin`, `Quarter` first.
539+
540+
#> Warning: Current temporal ordering may yield unexpected results.
541+
#> ℹ Suggest to sort by `Origin`, `Quarter` first.
542+
#> # A tsibble: 508 x 3 [1Q]
543+
#> # Key: Origin [4]
544+
#> Quarter Origin Arrivals
545+
#> <qtr> <chr> <dbl>
546+
#> 1 1981 Q1 Japan 12179.
547+
#> 2 1981 Q2 Japan 12742.
548+
#> 3 1981 Q3 Japan 13107.
549+
#> 4 1981 Q4 Japan 14065.
550+
#> 5 1982 Q1 Japan 14004.
551+
#> 6 1982 Q2 Japan 14564.
552+
#> 7 1982 Q3 Japan 14909.
553+
#> 8 1982 Q4 Japan 15542.
554+
#> 9 1983 Q1 Japan 16868.
555+
#> 10 1983 Q2 Japan 16584.
556+
#> # … with 498 more rows
557+
```
558+
517559
EOL

0 commit comments

Comments
 (0)