Skip to contents

Return a computation made on a vector using a sliding window.

Usage

slide(
  data,
  window.size,
  step,
  from = NULL,
  to = NULL,
  FUN,
  args = NULL,
  boundary = "left",
  parall = FALSE,
  thread.nb = NULL,
  print.count = 100,
  res.path = NULL,
  lib.path = NULL,
  verbose = TRUE,
  safer_check = TRUE
)

Arguments

data

Vector, matrix, table or array of numeric values (mode must be numeric). Inf not allowed. NA will be removed before computation.

window.size

Single numeric value indicating the width of the window sliding across data (in the same unit as data value).

step

Single numeric value indicating the step between each window (in the same unit as data value). Cannot be larger than window.size.

from

Single numeric value of the left boundary of the first sliding window. If NULL, min(data) is used. The first window will strictly have from or min(data) as left boundary.

to

Single numeric value of the right boundary of the last sliding window. If NULL, max(data) is used. Warning: (1) the final last window will not necessary have to|max(data) as right boundary. In fact the last window will be the one that contains to|max(data) for the first time, i.e., min[from|min(data) + window.size + n * step >= to|max(data)]; (2) In fact, the >= in min[from|min(data) + window.size + n * step >= to|max(data)] depends on the boundary argument (>= for "right" and > for "left"); (3) to have the rule (1) but for the center of the last window, use to argument as to = to|max(data) + window.size / 2.

FUN

Function or single character string indicating the name of the function to apply in each window. Example of function: FUN = mean. Example of character string: FUN = "mean".

args

Single character string of additional arguments of FUN (separated by a comma between the quotes). Example args = "na.rm = TRUE" for FUN = mean. Ignored if NULL.

boundary

Either "left" or "right". Indicates if the sliding window includes values equal to left boundary and exclude values equal to right boundary ("left") or the opposite ("right").

parall

Single logical value. Force parallelization ?

thread.nb

Single numeric value indicating the number of threads to use if ever parallelization is required. If NULL, all the available threads will be used. Ignored if parall is FALSE.

print.count

Single integer value. Print a working progress message every print.count during loops. BEWARE: can increase substantially the time to complete the process using a small value, like 10 for instance. Use Inf is no loop message desired.

res.path

Character string indicating the absolute pathway where the parallelization log file will be created if parallelization is used. If NULL, will be created in the R current directory.

lib.path

Character vector specifying the absolute pathways of the directories containing the required packages if not in the default directories. Ignored if NULL.

verbose

Single logical value. Display messages?

safer_check

Single logical value. Perform some "safer" checks (see https://github.com/safer-r)? If TRUE, checkings are performed before main code running: 1) R classical operators (like "<-") not overwritten by another package because of the R scope and 2) required functions and related packages effectively present in local R lybraries. Set to FALSE if this fonction is used inside another "safer" function to avoid pointless multiple checkings.

Value

A data frame containing :

- $left : the left boundary of each window (in the unit of the data argument).

- $right : the right boundary of each window (in the unit of data argument).

- $center : the center of each window (in the unit of data argument).

- $value : the computed value by the fun argument in each window).

Details

WARNINGS

The function uses two strategies, depending on the amout of memory required which depends on the data, window.size and step arguments. The first one uses lapply(), is generally fast but requires lots of memory. The second one uses a parallelized loop. The choice between the two strategies is automatic if parall argument is FALSE, and is forced toward parallelization if parall argument is TRUE.

The parall argument forces the parallelization, which is convenient when the data argument is big, because the lapply() function is sometimes slower than the parallelization.

Always use the env argument when slide() is used inside functions.

See also

slide

Author

Gael Millot <gael.millot@pasteur.fr>

Yushi Han <yushi.han2000@gmail.com>

Haiding Wang <wanghaiding442@gmail.com>

Examples

slide(data = c(1:10, 100:110, 500), window.size = 5, step = 2, FUN = length, boundary = "left")
#> 
#> slide JOB IGNITION
#> 
#> slide JOB END
#> 
#> TIME: 2024-09-03 17:28:29.469186
#> 
#> TOTAL TIME LAPSE: 0.034
#> 
#> 
#>     left right center value
#> 1      1     6    3.5     5
#> 2      3     8    5.5     5
#> 3      5    10    7.5     5
#> 4      7    12    9.5     4
#> 5      9    14   11.5     2
#> 6     11    16   13.5     0
#> 7     13    18   15.5     0
#> 8     15    20   17.5     0
#> 9     17    22   19.5     0
#> 10    19    24   21.5     0
#> 11    21    26   23.5     0
#> 12    23    28   25.5     0
#> 13    25    30   27.5     0
#> 14    27    32   29.5     0
#> 15    29    34   31.5     0
#> 16    31    36   33.5     0
#> 17    33    38   35.5     0
#> 18    35    40   37.5     0
#> 19    37    42   39.5     0
#> 20    39    44   41.5     0
#> 21    41    46   43.5     0
#> 22    43    48   45.5     0
#> 23    45    50   47.5     0
#> 24    47    52   49.5     0
#> 25    49    54   51.5     0
#> 26    51    56   53.5     0
#> 27    53    58   55.5     0
#> 28    55    60   57.5     0
#> 29    57    62   59.5     0
#> 30    59    64   61.5     0
#> 31    61    66   63.5     0
#> 32    63    68   65.5     0
#> 33    65    70   67.5     0
#> 34    67    72   69.5     0
#> 35    69    74   71.5     0
#> 36    71    76   73.5     0
#> 37    73    78   75.5     0
#> 38    75    80   77.5     0
#> 39    77    82   79.5     0
#> 40    79    84   81.5     0
#> 41    81    86   83.5     0
#> 42    83    88   85.5     0
#> 43    85    90   87.5     0
#> 44    87    92   89.5     0
#> 45    89    94   91.5     0
#> 46    91    96   93.5     0
#> 47    93    98   95.5     0
#> 48    95   100   97.5     0
#> 49    97   102   99.5     2
#> 50    99   104  101.5     4
#> 51   101   106  103.5     5
#> 52   103   108  105.5     5
#> 53   105   110  107.5     5
#> 54   107   112  109.5     4
#> 55   109   114  111.5     2
#> 56   111   116  113.5     0
#> 57   113   118  115.5     0
#> 58   115   120  117.5     0
#> 59   117   122  119.5     0
#> 60   119   124  121.5     0
#> 61   121   126  123.5     0
#> 62   123   128  125.5     0
#> 63   125   130  127.5     0
#> 64   127   132  129.5     0
#> 65   129   134  131.5     0
#> 66   131   136  133.5     0
#> 67   133   138  135.5     0
#> 68   135   140  137.5     0
#> 69   137   142  139.5     0
#> 70   139   144  141.5     0
#> 71   141   146  143.5     0
#> 72   143   148  145.5     0
#> 73   145   150  147.5     0
#> 74   147   152  149.5     0
#> 75   149   154  151.5     0
#> 76   151   156  153.5     0
#> 77   153   158  155.5     0
#> 78   155   160  157.5     0
#> 79   157   162  159.5     0
#> 80   159   164  161.5     0
#> 81   161   166  163.5     0
#> 82   163   168  165.5     0
#> 83   165   170  167.5     0
#> 84   167   172  169.5     0
#> 85   169   174  171.5     0
#> 86   171   176  173.5     0
#> 87   173   178  175.5     0
#> 88   175   180  177.5     0
#> 89   177   182  179.5     0
#> 90   179   184  181.5     0
#> 91   181   186  183.5     0
#> 92   183   188  185.5     0
#> 93   185   190  187.5     0
#> 94   187   192  189.5     0
#> 95   189   194  191.5     0
#> 96   191   196  193.5     0
#> 97   193   198  195.5     0
#> 98   195   200  197.5     0
#> 99   197   202  199.5     0
#> 100  199   204  201.5     0
#> 101  201   206  203.5     0
#> 102  203   208  205.5     0
#> 103  205   210  207.5     0
#> 104  207   212  209.5     0
#> 105  209   214  211.5     0
#> 106  211   216  213.5     0
#> 107  213   218  215.5     0
#> 108  215   220  217.5     0
#> 109  217   222  219.5     0
#> 110  219   224  221.5     0
#> 111  221   226  223.5     0
#> 112  223   228  225.5     0
#> 113  225   230  227.5     0
#> 114  227   232  229.5     0
#> 115  229   234  231.5     0
#> 116  231   236  233.5     0
#> 117  233   238  235.5     0
#> 118  235   240  237.5     0
#> 119  237   242  239.5     0
#> 120  239   244  241.5     0
#> 121  241   246  243.5     0
#> 122  243   248  245.5     0
#> 123  245   250  247.5     0
#> 124  247   252  249.5     0
#> 125  249   254  251.5     0
#> 126  251   256  253.5     0
#> 127  253   258  255.5     0
#> 128  255   260  257.5     0
#> 129  257   262  259.5     0
#> 130  259   264  261.5     0
#> 131  261   266  263.5     0
#> 132  263   268  265.5     0
#> 133  265   270  267.5     0
#> 134  267   272  269.5     0
#> 135  269   274  271.5     0
#> 136  271   276  273.5     0
#> 137  273   278  275.5     0
#> 138  275   280  277.5     0
#> 139  277   282  279.5     0
#> 140  279   284  281.5     0
#> 141  281   286  283.5     0
#> 142  283   288  285.5     0
#> 143  285   290  287.5     0
#> 144  287   292  289.5     0
#> 145  289   294  291.5     0
#> 146  291   296  293.5     0
#> 147  293   298  295.5     0
#> 148  295   300  297.5     0
#> 149  297   302  299.5     0
#> 150  299   304  301.5     0
#> 151  301   306  303.5     0
#> 152  303   308  305.5     0
#> 153  305   310  307.5     0
#> 154  307   312  309.5     0
#> 155  309   314  311.5     0
#> 156  311   316  313.5     0
#> 157  313   318  315.5     0
#> 158  315   320  317.5     0
#> 159  317   322  319.5     0
#> 160  319   324  321.5     0
#> 161  321   326  323.5     0
#> 162  323   328  325.5     0
#> 163  325   330  327.5     0
#> 164  327   332  329.5     0
#> 165  329   334  331.5     0
#> 166  331   336  333.5     0
#> 167  333   338  335.5     0
#> 168  335   340  337.5     0
#> 169  337   342  339.5     0
#> 170  339   344  341.5     0
#> 171  341   346  343.5     0
#> 172  343   348  345.5     0
#> 173  345   350  347.5     0
#> 174  347   352  349.5     0
#> 175  349   354  351.5     0
#> 176  351   356  353.5     0
#> 177  353   358  355.5     0
#> 178  355   360  357.5     0
#> 179  357   362  359.5     0
#> 180  359   364  361.5     0
#> 181  361   366  363.5     0
#> 182  363   368  365.5     0
#> 183  365   370  367.5     0
#> 184  367   372  369.5     0
#> 185  369   374  371.5     0
#> 186  371   376  373.5     0
#> 187  373   378  375.5     0
#> 188  375   380  377.5     0
#> 189  377   382  379.5     0
#> 190  379   384  381.5     0
#> 191  381   386  383.5     0
#> 192  383   388  385.5     0
#> 193  385   390  387.5     0
#> 194  387   392  389.5     0
#> 195  389   394  391.5     0
#> 196  391   396  393.5     0
#> 197  393   398  395.5     0
#> 198  395   400  397.5     0
#> 199  397   402  399.5     0
#> 200  399   404  401.5     0
#> 201  401   406  403.5     0
#> 202  403   408  405.5     0
#> 203  405   410  407.5     0
#> 204  407   412  409.5     0
#> 205  409   414  411.5     0
#> 206  411   416  413.5     0
#> 207  413   418  415.5     0
#> 208  415   420  417.5     0
#> 209  417   422  419.5     0
#> 210  419   424  421.5     0
#> 211  421   426  423.5     0
#> 212  423   428  425.5     0
#> 213  425   430  427.5     0
#> 214  427   432  429.5     0
#> 215  429   434  431.5     0
#> 216  431   436  433.5     0
#> 217  433   438  435.5     0
#> 218  435   440  437.5     0
#> 219  437   442  439.5     0
#> 220  439   444  441.5     0
#> 221  441   446  443.5     0
#> 222  443   448  445.5     0
#> 223  445   450  447.5     0
#> 224  447   452  449.5     0
#> 225  449   454  451.5     0
#> 226  451   456  453.5     0
#> 227  453   458  455.5     0
#> 228  455   460  457.5     0
#> 229  457   462  459.5     0
#> 230  459   464  461.5     0
#> 231  461   466  463.5     0
#> 232  463   468  465.5     0
#> 233  465   470  467.5     0
#> 234  467   472  469.5     0
#> 235  469   474  471.5     0
#> 236  471   476  473.5     0
#> 237  473   478  475.5     0
#> 238  475   480  477.5     0
#> 239  477   482  479.5     0
#> 240  479   484  481.5     0
#> 241  481   486  483.5     0
#> 242  483   488  485.5     0
#> 243  485   490  487.5     0
#> 244  487   492  489.5     0
#> 245  489   494  491.5     0
#> 246  491   496  493.5     0
#> 247  493   498  495.5     0
#> 248  495   500  497.5     0
#> 249  497   502  499.5     1

slide(data = c(1:10, 100:110, 500), window.size = 5, step = 2, FUN = length, boundary = "right") # effect of boundary argument
#> 
#> slide JOB IGNITION
#> 
#> slide JOB END
#> 
#> TIME: 2024-09-03 17:28:29.561583
#> 
#> TOTAL TIME LAPSE: 0.012
#> 
#> 
#>     left right center value
#> 1      1     6    3.5     5
#> 2      3     8    5.5     5
#> 3      5    10    7.5     5
#> 4      7    12    9.5     3
#> 5      9    14   11.5     1
#> 6     11    16   13.5     0
#> 7     13    18   15.5     0
#> 8     15    20   17.5     0
#> 9     17    22   19.5     0
#> 10    19    24   21.5     0
#> 11    21    26   23.5     0
#> 12    23    28   25.5     0
#> 13    25    30   27.5     0
#> 14    27    32   29.5     0
#> 15    29    34   31.5     0
#> 16    31    36   33.5     0
#> 17    33    38   35.5     0
#> 18    35    40   37.5     0
#> 19    37    42   39.5     0
#> 20    39    44   41.5     0
#> 21    41    46   43.5     0
#> 22    43    48   45.5     0
#> 23    45    50   47.5     0
#> 24    47    52   49.5     0
#> 25    49    54   51.5     0
#> 26    51    56   53.5     0
#> 27    53    58   55.5     0
#> 28    55    60   57.5     0
#> 29    57    62   59.5     0
#> 30    59    64   61.5     0
#> 31    61    66   63.5     0
#> 32    63    68   65.5     0
#> 33    65    70   67.5     0
#> 34    67    72   69.5     0
#> 35    69    74   71.5     0
#> 36    71    76   73.5     0
#> 37    73    78   75.5     0
#> 38    75    80   77.5     0
#> 39    77    82   79.5     0
#> 40    79    84   81.5     0
#> 41    81    86   83.5     0
#> 42    83    88   85.5     0
#> 43    85    90   87.5     0
#> 44    87    92   89.5     0
#> 45    89    94   91.5     0
#> 46    91    96   93.5     0
#> 47    93    98   95.5     0
#> 48    95   100   97.5     1
#> 49    97   102   99.5     3
#> 50    99   104  101.5     5
#> 51   101   106  103.5     5
#> 52   103   108  105.5     5
#> 53   105   110  107.5     5
#> 54   107   112  109.5     3
#> 55   109   114  111.5     1
#> 56   111   116  113.5     0
#> 57   113   118  115.5     0
#> 58   115   120  117.5     0
#> 59   117   122  119.5     0
#> 60   119   124  121.5     0
#> 61   121   126  123.5     0
#> 62   123   128  125.5     0
#> 63   125   130  127.5     0
#> 64   127   132  129.5     0
#> 65   129   134  131.5     0
#> 66   131   136  133.5     0
#> 67   133   138  135.5     0
#> 68   135   140  137.5     0
#> 69   137   142  139.5     0
#> 70   139   144  141.5     0
#> 71   141   146  143.5     0
#> 72   143   148  145.5     0
#> 73   145   150  147.5     0
#> 74   147   152  149.5     0
#> 75   149   154  151.5     0
#> 76   151   156  153.5     0
#> 77   153   158  155.5     0
#> 78   155   160  157.5     0
#> 79   157   162  159.5     0
#> 80   159   164  161.5     0
#> 81   161   166  163.5     0
#> 82   163   168  165.5     0
#> 83   165   170  167.5     0
#> 84   167   172  169.5     0
#> 85   169   174  171.5     0
#> 86   171   176  173.5     0
#> 87   173   178  175.5     0
#> 88   175   180  177.5     0
#> 89   177   182  179.5     0
#> 90   179   184  181.5     0
#> 91   181   186  183.5     0
#> 92   183   188  185.5     0
#> 93   185   190  187.5     0
#> 94   187   192  189.5     0
#> 95   189   194  191.5     0
#> 96   191   196  193.5     0
#> 97   193   198  195.5     0
#> 98   195   200  197.5     0
#> 99   197   202  199.5     0
#> 100  199   204  201.5     0
#> 101  201   206  203.5     0
#> 102  203   208  205.5     0
#> 103  205   210  207.5     0
#> 104  207   212  209.5     0
#> 105  209   214  211.5     0
#> 106  211   216  213.5     0
#> 107  213   218  215.5     0
#> 108  215   220  217.5     0
#> 109  217   222  219.5     0
#> 110  219   224  221.5     0
#> 111  221   226  223.5     0
#> 112  223   228  225.5     0
#> 113  225   230  227.5     0
#> 114  227   232  229.5     0
#> 115  229   234  231.5     0
#> 116  231   236  233.5     0
#> 117  233   238  235.5     0
#> 118  235   240  237.5     0
#> 119  237   242  239.5     0
#> 120  239   244  241.5     0
#> 121  241   246  243.5     0
#> 122  243   248  245.5     0
#> 123  245   250  247.5     0
#> 124  247   252  249.5     0
#> 125  249   254  251.5     0
#> 126  251   256  253.5     0
#> 127  253   258  255.5     0
#> 128  255   260  257.5     0
#> 129  257   262  259.5     0
#> 130  259   264  261.5     0
#> 131  261   266  263.5     0
#> 132  263   268  265.5     0
#> 133  265   270  267.5     0
#> 134  267   272  269.5     0
#> 135  269   274  271.5     0
#> 136  271   276  273.5     0
#> 137  273   278  275.5     0
#> 138  275   280  277.5     0
#> 139  277   282  279.5     0
#> 140  279   284  281.5     0
#> 141  281   286  283.5     0
#> 142  283   288  285.5     0
#> 143  285   290  287.5     0
#> 144  287   292  289.5     0
#> 145  289   294  291.5     0
#> 146  291   296  293.5     0
#> 147  293   298  295.5     0
#> 148  295   300  297.5     0
#> 149  297   302  299.5     0
#> 150  299   304  301.5     0
#> 151  301   306  303.5     0
#> 152  303   308  305.5     0
#> 153  305   310  307.5     0
#> 154  307   312  309.5     0
#> 155  309   314  311.5     0
#> 156  311   316  313.5     0
#> 157  313   318  315.5     0
#> 158  315   320  317.5     0
#> 159  317   322  319.5     0
#> 160  319   324  321.5     0
#> 161  321   326  323.5     0
#> 162  323   328  325.5     0
#> 163  325   330  327.5     0
#> 164  327   332  329.5     0
#> 165  329   334  331.5     0
#> 166  331   336  333.5     0
#> 167  333   338  335.5     0
#> 168  335   340  337.5     0
#> 169  337   342  339.5     0
#> 170  339   344  341.5     0
#> 171  341   346  343.5     0
#> 172  343   348  345.5     0
#> 173  345   350  347.5     0
#> 174  347   352  349.5     0
#> 175  349   354  351.5     0
#> 176  351   356  353.5     0
#> 177  353   358  355.5     0
#> 178  355   360  357.5     0
#> 179  357   362  359.5     0
#> 180  359   364  361.5     0
#> 181  361   366  363.5     0
#> 182  363   368  365.5     0
#> 183  365   370  367.5     0
#> 184  367   372  369.5     0
#> 185  369   374  371.5     0
#> 186  371   376  373.5     0
#> 187  373   378  375.5     0
#> 188  375   380  377.5     0
#> 189  377   382  379.5     0
#> 190  379   384  381.5     0
#> 191  381   386  383.5     0
#> 192  383   388  385.5     0
#> 193  385   390  387.5     0
#> 194  387   392  389.5     0
#> 195  389   394  391.5     0
#> 196  391   396  393.5     0
#> 197  393   398  395.5     0
#> 198  395   400  397.5     0
#> 199  397   402  399.5     0
#> 200  399   404  401.5     0
#> 201  401   406  403.5     0
#> 202  403   408  405.5     0
#> 203  405   410  407.5     0
#> 204  407   412  409.5     0
#> 205  409   414  411.5     0
#> 206  411   416  413.5     0
#> 207  413   418  415.5     0
#> 208  415   420  417.5     0
#> 209  417   422  419.5     0
#> 210  419   424  421.5     0
#> 211  421   426  423.5     0
#> 212  423   428  425.5     0
#> 213  425   430  427.5     0
#> 214  427   432  429.5     0
#> 215  429   434  431.5     0
#> 216  431   436  433.5     0
#> 217  433   438  435.5     0
#> 218  435   440  437.5     0
#> 219  437   442  439.5     0
#> 220  439   444  441.5     0
#> 221  441   446  443.5     0
#> 222  443   448  445.5     0
#> 223  445   450  447.5     0
#> 224  447   452  449.5     0
#> 225  449   454  451.5     0
#> 226  451   456  453.5     0
#> 227  453   458  455.5     0
#> 228  455   460  457.5     0
#> 229  457   462  459.5     0
#> 230  459   464  461.5     0
#> 231  461   466  463.5     0
#> 232  463   468  465.5     0
#> 233  465   470  467.5     0
#> 234  467   472  469.5     0
#> 235  469   474  471.5     0
#> 236  471   476  473.5     0
#> 237  473   478  475.5     0
#> 238  475   480  477.5     0
#> 239  477   482  479.5     0
#> 240  479   484  481.5     0
#> 241  481   486  483.5     0
#> 242  483   488  485.5     0
#> 243  485   490  487.5     0
#> 244  487   492  489.5     0
#> 245  489   494  491.5     0
#> 246  491   496  493.5     0
#> 247  493   498  495.5     0
#> 248  495   500  497.5     1

if (FALSE) {
slide(data = c(1:10, 100:110, 500), window.size = 5, step = 2, FUN = length, boundary = "left", parall = TRUE, thread.nb = 2) # effect of parall argument
}