Skip to content

Commit c61c573

Browse files
committed
Merge branch 'master' into frollapply-nneg
2 parents 8eb449d + 9b421c8 commit c61c573

17 files changed

+247
-114
lines changed

NEWS.md

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,7 @@
3232
```
3333
Additionally argument names in `frollapply` has been renamed from `x` to `X` and `n` to `N` to avoid conflicts with common argument names that may be passed to `...`, aligning to base R API of `lapply`. `x` and `n` continue to work with a warning, for now.
3434
35-
5. Adaptive rolling functions no longer tolerate `NA`s and negative values passed to `n` argument.
36-
```r
37-
n = c(2,NA,2)
38-
frollsum(1:3, n, adaptive=TRUE)
39-
#Error in froll(fun = "sum", x = x, n = n, fill = fill, algo = algo, align = align, :
40-
# 'n' must be non-negative integer values (>= 0)
41-
```
42-
If for some reason previous `NA`s behavior is needed, it can be achieved by replacing `NA`s with a value big enough
43-
```r
44-
n = nafill(c(2,NA,2), fill=.Machine$integer.max)
45-
frollsum(1:3, n, adaptive=TRUE)
46-
```
35+
5. Negative and missing values of `n` argument of adaptive rolling functions trigger an error.
4736
4837
### NOTICE OF INTENDED FUTURE POTENTIAL BREAKING CHANGES
4938
@@ -223,6 +212,7 @@
223212
#[1] TRUE
224213
```
225214

215+
<<<<<<< HEAD
226216
18. New `frolladapt` helper function has been added to aid in preparation of adaptive length of rolling window width when dealing with _irregularly spaced ordered data_. This lets the user to apply a rolling function over a period without having to deal with gaps in a data where some periods might be missing, [#3241](https://github.com/Rdatatable/data.table/issues/3241). Thanks to @jangorecki for implementation.
227217
```r
228218
idx = as.Date("2025-09-08") + c(0,1,4,5,6,7,9,10,14)
@@ -256,6 +246,41 @@ dt
256246
#8: 2025-09-18 8 7 7.5
257247
#9: 2025-09-22 9 8 9.0
258248
```
249+
=======
250+
18. New helper `frolladapt` to facilitate applying rolling functions over windows of fixed calendar-time width in irregularly-spaced data sets, thereby bypassing the need to "augment" such data with placeholder rows, [#3241](https://github.com/Rdatatable/data.table/issues/3241). Thanks to @jangorecki for implementation.
251+
```r
252+
idx = as.Date("2025-09-05") + c(0,4,7,8,9,10,12,13,17)
253+
dt = data.table(index=idx, value=seq_along(idx))
254+
dt
255+
# index value
256+
# <Date> <int>
257+
#1: 2025-09-05 1
258+
#2: 2025-09-09 2
259+
#3: 2025-09-12 3
260+
#4: 2025-09-13 4
261+
#5: 2025-09-14 5
262+
#6: 2025-09-15 6
263+
#7: 2025-09-17 7
264+
#8: 2025-09-18 8
265+
#9: 2025-09-22 9
266+
dt[, c("rollmean3","rollmean3days") := list(
267+
frollmean(value, 3),
268+
frollmean(value, frolladapt(index, 3), adaptive=TRUE)
269+
)]
270+
dt
271+
# index value rollmean3 rollmean3days
272+
# <Date> <int> <num> <num>
273+
#1: 2025-09-05 1 NA NA
274+
#2: 2025-09-09 2 NA 2.0
275+
#3: 2025-09-12 3 2 3.0
276+
#4: 2025-09-13 4 3 3.5
277+
#5: 2025-09-14 5 4 4.0
278+
#6: 2025-09-15 6 5 5.0
279+
#7: 2025-09-17 7 6 6.5
280+
#8: 2025-09-18 8 7 7.5
281+
#9: 2025-09-22 9 8 9.0
282+
```
283+
>>>>>>> master
259284

260285
### BUG FIXES
261286

man/frolladapt.Rd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
frolladapt(x, n, align="right", partial=FALSE, give.names=FALSE)
99
}
1010
\arguments{
11-
\item{x}{ Integer. Other objects of type numeric (including \code{Date}, \code{POSIXct} and any others numeric-based class) will be coerced to integer, which, for example, in case of \code{POSIXct} means truncating to whole seconds. Must be sorted, have no duplicate and have no missing values. }
12-
\item{n}{ Integer, positive, vector giving rolling window size(s). This is the \emph{total} number of included values in aggregate function. Value corresponds to unit of \code{x}. When \code{x} is a \code{POSIXct} then \code{n} are seconds, when \code{x} is a \code{Date} then \code{n} are days. }
13-
\item{align}{ Character, default \code{"right"}. Other alignments than the default have not yet been implemented. }
11+
\item{x}{ Integer. Must be sorted with no duplicates or missing values. Other objects with numeric storage (including most commonly \code{Date} and \code{POSIXct}) will be coerced to integer, which, for example, in case of \code{POSIXct} means truncating to whole seconds. }
12+
\item{n}{ Integer vector giving rolling positive window size(s). Up to \code{n} values nearest to each value of \code{x}, with distance in the units of \code{x} and according to the window implied by \code{align}, are included in each rolling aggregation window. Thus when \code{x} is a \code{POSIXct}, \code{n} are seconds, and when \code{x} is a \code{Date}, \code{n} are days. }
13+
\item{align}{ Character, default \code{"right"}. Other alignments have not yet been implemented. }
1414
\item{partial}{ Logical, default \code{FALSE}. Should the rolling window size(s) provided in \code{n} be trimmed to available observations. For details see \code{\link{froll}}. }
1515
\item{give.names}{ Logical, default \code{FALSE}. When \code{TRUE}, names are automatically generated corresponding to names of \code{n}. If answer is an integer vector, then the argument is ignored, see examples. }
1616
}

src/frollR.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ SEXP frolladapt(SEXP xobj, SEXP kobj, SEXP partial) {
216216
int n = INTEGER(kobj)[0];
217217
if (n < 1L)
218218
error(_("'n' must be positive integer values (>= 1)"));
219-
int *x = INTEGER_RO(xobj);
219+
const int *x = INTEGER_RO(xobj);
220220
int64_t len = XLENGTH(xobj); // can be 0
221221

222222
if (len && x[0] == NA_INTEGER)
@@ -239,7 +239,7 @@ SEXP frolladapt(SEXP xobj, SEXP kobj, SEXP partial) {
239239
if (an > n) {
240240
error(_("internal error: an > n, should not increment i in the first place")); // # nocov
241241
} else if (an == n) { // an is same size as n, so we either have no gaps or will need to shrink an by j++
242-
if (lhs == rhs+n-1) { // no gaps - or a k gaps and a k dups?
242+
if (lhs == rhs+n-1) { // no gaps - or a n gaps and a n dups?
243243
ians[i] = n; // could skip if pre-fill
244244
i++;
245245
j++;

vignettes/datatable-joins.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ Products[ProductReceived,
169169
on = list(id = product_id)]
170170
```
171171

172-
- Wrapping the related columns in the `data.table` `list` alias `.`.
172+
- Wrapping the related columns in the `list` alias `.`.
173173

174174
```{r, eval=FALSE}
175175
Products[ProductReceived,

vignettes/datatable-reshape.Rmd

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ However, there are situations we might run into where the desired operation is n
143143

144144
```{r}
145145
s2 <- "family_id age_mother name_child1 name_child2 name_child3 gender_child1 gender_child2 gender_child3
146-
1 30 Ben Anna NA 1 2 NA
147-
2 27 Tom NA NA 2 NA NA
148-
3 26 Lia Sam Amy 2 2 1
149-
4 32 Max Zoe Joe 1 1 1
150-
5 29 Dan Eva NA 2 1 NA"
146+
1 30 Ben Anna NA 1 2 NA
147+
2 27 Tom NA NA 2 NA NA
148+
3 26 Lia Sam Amy 2 2 1
149+
4 32 Max Zoe Joe 1 1 1
150+
5 29 Dan Eva NA 2 1 NA"
151151
DT <- fread(s2)
152152
DT
153153
## 1 = female, 2 = male

vignettes/datatable-secondary-indices-and-auto-indexing.Rmd

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ knitr::opts_chunk$set(
2424
.old.th = setDTthreads(1)
2525
```
2626

27-
This vignette assumes that the reader is familiar with data.table's `[i, j, by]` syntax, and how to perform fast key based subsets. If you're not familiar with these concepts, please read the [`vignette("datatable-intro", package="data.table")`](datatable-intro.html), [`vignette("datatable-reference-semantics", package="data.table")`](datatable-reference-semantics.html), and [`vignette("datatable-keys-fast-subset", package="data.table")`](datatable-keys-fast-subset.html) vignettes first.
27+
This vignette assumes that the reader is familiar with data.table's `[i, j, by]` syntax, and how to perform fast key based subsets. If you're not familiar with these concepts, please read the following vignettes first:
28+
29+
- [`vignette("datatable-intro", package="data.table")`](datatable-intro.html)
30+
- [`vignette("datatable-reference-semantics", package="data.table")`](datatable-reference-semantics.html)
31+
- [`vignette("datatable-keys-fast-subset", package="data.table")`](datatable-keys-fast-subset.html)
2832

2933
***
3034

vignettes/fr/datatable-benchmarking.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ h2 {
2020

2121
```{r echo=FALSE, file='../_translation_links.R'}
2222
```
23-
`r .write.translation.links("Translations of this document are available in: %s")`
23+
`r .write.translation.links("Une traduction de ce document est disponible en : %s")`
2424

2525
Ce document a pour but de guider la mesure de la performance de `data.table`. Il centralise la documentation des meilleures pratiques et des pièges à éviter.
2626

vignettes/fr/datatable-faq.Rmd

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@ vignette: >
1616
h2 {
1717
font-size: 20px;
1818
}
19-
#TOC { width: 100%; }
19+
20+
#TOC {
21+
border: 1px solid #ccc;
22+
border-radius: 5px;
23+
padding-left: 1em;
24+
background: #f6f6f6;
25+
width: 100%;
26+
}
2027
</style>
2128

2229
```{r echo=FALSE, file='../_translation_links.R'}
2330
```
24-
`r .write.translation.links("Translations of this document are available in: %s")`
31+
`r .write.translation.links("Une traduction de ce document est disponible en : %s")`
2532

2633
```{r, echo = FALSE, message = FALSE}
2734
library(data.table)
@@ -582,7 +589,10 @@ DT[ , b := rnorm(5)] # « remplace » la colonne entière par une colonne num
582589

583590
## Lecture de data.table à partir d'un fichier RDS ou RData
584591

585-
`*.RDS` et `*.RData` sont des types de fichiers qui permettent de stocker efficacement des objets R en mémoire sur le disque. Cependant, le stockage de data.table dans le fichier binaire perd sa sur-allocation de colonnes. Ce n'est pas très grave -- votre data.table sera copié en mémoire lors de la prochaine opération *par référence* et lancera un avertissement. Il est donc recommandé d'appeler `setalloccol()` sur chaque data.table chargée avec les appels `readRDS()` ou `load()`.
592+
`*.RDS` et `*.RData` sont des types de fichiers qui permettent de stocker efficacement des objets R en mémoire sur le disque. Cependant, le stockage de data.table dans le fichier binaire perd sa sur-allocation de colonnes (voir aussi `?truelength`). Ce n'est pas très grave -- votre `data.table` sera copié en mémoire lors de la prochaine opération _par référence_ et lancera un avertissement.
593+
C'est pourquoi il est recommandé d'appeler `setDT()` sur chaque `data.table` chargé par un appel à `readRDS()` ou `load()` afin de restaurer ses attributs internes. Si vous avez simplement besoin de préallouer de l'espace pour de nouvelles colonnes, vous pouvez également utiliser `setalloccol()`.
594+
595+
Pour d'autres informations, voir `?setDT` et `?truelength`.
586596

587597
# Questions générales sur le package
588598

vignettes/fr/datatable-importing.Rmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ h2 {
1717

1818
```{r echo=FALSE, file='../_translation_links.R'}
1919
```
20-
`r .write.translation.links("Translations of this document are available in: %s")`
20+
`r .write.translation.links("Une traduction de ce document est disponible en : %s")`
2121

2222
Ce document se concentre sur l'utilisation de `data.table` comme dépendance dans d'autres packages R. Si vous souhaitez utiliser le code C de `data.table` à partir d'une application non-R, ou appeler directement ses fonctions C, passez à la [dernière section](#non-r-API) de cette vignette.
2323

@@ -75,7 +75,7 @@ dt2 = aggr(dt)
7575
stopifnot(nrow(dt2) < 100)
7676
```
7777

78-
Lorsque vous testez votre package, vous pouvez utiliser `R CMD check --no-stop-on-test-error`, qui continuera après une erreur et exécutera tous vos tests (au lieu de s'arrêter à la première ligne de script qui a échoué).
78+
Lorsque vous testez votre package, vous pouvez utiliser `R CMD check --no-stop-on-test-error`, qui continuera après une erreur et exécutera tous vos tests (au lieu de s'arrêter à la première ligne du script qui a échoué).
7979

8080
## Tester en utilisant `testthat`
8181

vignettes/fr/datatable-intro.Rmd

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ vignette: >
1111

1212
```{r echo=FALSE, file='../_translation_links.R'}
1313
```
14-
`r .write.translation.links("Translations of this document are available in: %s")`
14+
`r .write.translation.links("Une traduction de ce document est disponible en : %s")`
1515

1616
```{r, echo = FALSE, message = FALSE}
1717
require(data.table)
@@ -101,7 +101,7 @@ Vous pouvez aussi convertir des objets existants en une `data.table` en utilisan
101101
getOption("datatable.print.nrows")
102102
```
103103
104-
* `data.table` ne définit ni n'utilise jamais de *nom de ligne*. Nous verrons pourquoi dans la vignette *"Sous-ensemble basé sur des clés et recherche binaire rapide"*.
104+
* `data.table` ne définit ni n'utilise jamais de *nom de ligne*. Nous verrons pourquoi dans la [`vignette("datatable-keys-fast-subset", package="data.table")`](datatable-keys-fast-subset.html).
105105
106106
### b) Forme générale - dans quel sens la 'data.table' est-elle *étendue* ? {#enhanced-1b}
107107
@@ -479,7 +479,7 @@ ans
479479

480480
**Clés :** actuellement `keyby` en fait un peu plus que *simplement trier*. Il *définit une clé* également après le tri en initialisant un `attribute` appelé `sorted`.
481481

482-
Nous en apprendrons plus au sujet des `clés` dans la vignette *Clés et sous-ensembles basés sur la recherche binaire rapide*; pour l'instant, tout ce que vous devez savoir est que vous pouvez utiliser `keyby` pour trier automatiquement le résultat selon les colonnes spécifiées dans `by`.
482+
Nous en apprendrons plus au sujet des `clés` dans la [`vignette("datatable-keys-fast-subset", package="data.table")`](datatable-keys-fast-subset.html); pour l'instant, tout ce que vous devez savoir est que vous pouvez utiliser `keyby` pour trier automatiquement le résultat selon les colonnes spécifiées dans `by`.
483483

484484
### c) Chaînage
485485

@@ -643,6 +643,31 @@ DT[, print(list(c(a,b))), by = ID] # (2)
643643

644644
Dans (1), pour chaque groupe, un vecteur est renvoyé, de longueur = 6,4,2 ici. Néanmoins, (2) renvoie une liste de longueur 1 pour chaque groupe, dont chaque premier élément contient des vecteurs de longueur 6,4,2. C'est pourquoi, (1) a pour longueur totale `6+4+2 =`r 6+4+2``, alors que (2) renvoie `1+1+1=`r 1+1+1``.
645645

646+
La flexibilité de j nous permet de ranger toute liste d'objets comme un élément de data.table. Par exemple lorsque des modèles statistiques sont adaptés aux groupes, ils peuvent être placés dans un data.table. Le code est concis et facile à comprendre.
647+
648+
```{r}
649+
## les vols long courrier couvrent-ils les retards au départ davantage que les vols à courte distance ?
650+
## la couverture varie-t-elle selon les mois ?
651+
flights[, `:=`(makeup = dep_delay - arr_delay)]
652+
653+
makeup.models <- flights[, .(fit = list(lm(makeup ~ distance))), by = .(month)]
654+
makeup.models[, .(coefdist = coef(fit[[1]])[2], rsq = summary(fit[[1]])$r.squared), by = .(month)]
655+
```
656+
657+
Avec les data.frames il nous faut un code plus complexe pour obtenir le même résultat.
658+
659+
```{r}
660+
setDF(flights)
661+
flights.split <- split(flights, f = flights$month)
662+
makeup.models.list <- lapply(flights.split, function(df) c(month = df$month[1], fit = list(lm(makeup ~ distance, data = df))))
663+
makeup.models.df <- do.call(rbind, makeup.models.list)
664+
data.frame(t(sapply(
665+
makeup.models.df[, "fit"],
666+
function(model) c(coefdist = coef(model)[2L], rsq = summary(model)$r.squared)
667+
)))
668+
setDT(flights)
669+
```
670+
646671
## Résumé
647672

648673
La forme générale de la syntaxe de `data.table` est :
@@ -659,7 +684,7 @@ Jusqu'ici nous avons vu que,
659684

660685
* Nous pouvons également trier un `data.table` en utilisant `order()`, qui utilise en interne l’algorithme de tri rapide de data.table pour de meilleures performances.
661686

662-
Nous pouvons faire beaucoup plus dans `i` en créant une `data.table` avec clés, ce qui permet de réaliser rapidement les sous-ensembles et les jointures. Nous verrons cela dans les vignettes *"Clés et sous-ensembles basés sur la recherche binaire rapide"* et *"Jointures et jointures liées au temps"*.
687+
Nous pouvons faire beaucoup plus dans `i` en créant une `data.table` avec clés, ce qui permet de réaliser rapidement les sous-ensembles et les jointures. Nous verrons cela dans les [`vignette("datatable-keys-fast-subset", package="data.table")`](datatable-keys-fast-subset.html) et [`vignette("datatable-joins", package="data.table")`](datatable-joins.html).
663688

664689
#### En utilisant `j` :
665690

@@ -693,7 +718,7 @@ Nous pouvons faire beaucoup plus dans `i` en créant une `data.table` avec clés
693718

694719
Tant que `j` renvoie un objet `list`, chaque élément de la liste va devenir une colonne du `data.table` résultant.
695720

696-
Nous verrons dans la vignette suivante comment *ajouter / mettre à jour / supprimer* des colonnes *par référence* et comment les combiner avec `i` et `by` .
721+
Nous verrons dans la prochaine [(`vignette("datatable-reference-semantics", package="data.table")`)](datatable-reference-semantics.html) comment *ajouter / mettre à jour / supprimer* des colonnes *par référence* et comment les combiner avec `i` et `by` .
697722

698723
***
699724

0 commit comments

Comments
 (0)