fcumsum.Rdfcumsum is a generic function that computes the (column-wise) cumulative sum of x, (optionally) grouped by g and/or ordered by o. Several options to deal with missing values are provided.
fcumsum(x, ...)
# Default S3 method
fcumsum(x, g = NULL, o = NULL, na.rm = .op[["na.rm"]], fill = FALSE, check.o = TRUE, ...)
# S3 method for class 'matrix'
fcumsum(x, g = NULL, o = NULL, na.rm = .op[["na.rm"]], fill = FALSE, check.o = TRUE, ...)
# S3 method for class 'data.frame'
fcumsum(x, g = NULL, o = NULL, na.rm = .op[["na.rm"]], fill = FALSE, check.o = TRUE, ...)
# Methods for indexed data / compatibility with plm:
# S3 method for class 'pseries'
fcumsum(x, na.rm = .op[["na.rm"]], fill = FALSE, shift = "time", ...)
# S3 method for class 'pdata.frame'
fcumsum(x, na.rm = .op[["na.rm"]], fill = FALSE, shift = "time", ...)
# Methods for grouped data frame / compatibility with dplyr:
# S3 method for class 'grouped_df'
fcumsum(x, o = NULL, na.rm = .op[["na.rm"]], fill = FALSE, check.o = TRUE,
keep.ids = TRUE, ...)a numeric vector / time series, (time series) matrix, data frame, 'indexed_series' ('pseries'), 'indexed_frame' ('pdata.frame') or grouped data frame ('grouped_df').
a factor, GRP object, or atomic vector / list of vectors (internally grouped with group) used to group x.
a vector or list of vectors providing the order in which the elements of x are cumulatively summed. Will be passed to radixorderv unless check.o = FALSE.
logical. Skip missing values in x. Defaults to TRUE and implemented at very little computational cost.
if na.rm = TRUE, setting fill = TRUE will overwrite missing values with the previous value of the cumulative sum, starting from 0.
logical. Programmers option: FALSE prevents passing o to radixorderv, requiring o to be a valid ordering vector that is integer typed with each element in the range [1, length(x)]. This gives some extra speed, but will terminate R if any element of o is too large or too small.
pseries / pdata.frame methods: character. "time" or "row". See flag for details. The argument here does not control 'shifting' of data but rather the order in which elements are summed.
pdata.frame / grouped_df methods: Logical. Drop all identifiers from the output (which includes all grouping variables and variables passed to o). Note: For grouped / panel data frames identifiers are dropped, but the "groups" / "index" attributes are kept.
arguments to be passed to or from other methods.
If na.rm = FALSE, fcumsum works like cumsum and propagates missing values. The default na.rm = TRUE skips missing values and computes the cumulative sum on the non-missing values. Missing values are kept. If fill = TRUE, missing values are replaced with the previous value of the cumulative sum (starting from 0), computed on the non-missing values.
By default the cumulative sum is computed in the order in which elements appear in x. If o is provided, the cumulative sum is computed in the order given by radixorderv(o), without the need to first sort x. This applies as well if groups are used (g), in which case the cumulative sum is computed separately in each group.
The pseries and pdata.frame methods assume that the last factor in the index is the time-variable and the rest are grouping variables. The time-variable is passed to radixorderv and used for ordered computation, so that cumulative sums are accurately computed regardless of whether the panel-data is ordered or balanced.
fcumsum explicitly supports integers. Integers in R are bounded at bounded at +-2,147,483,647, and an integer overflow error will be provided if the cumulative sum (within any group) exceeds +-2,147,483,647. In that case data should be converted to double beforehand.
the cumulative sum of values in x, (optionally) grouped by g and/or ordered by o. See Details and Examples.
## Non-grouped
fcumsum(AirPassengers)
#> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#> 1949 112 230 362 491 612 747 895 1043 1179 1298 1402 1520
#> 1950 1635 1761 1902 2037 2162 2311 2481 2651 2809 2942 3056 3196
#> 1951 3341 3491 3669 3832 4004 4182 4381 4580 4764 4926 5072 5238
#> 1952 5409 5589 5782 5963 6146 6364 6594 6836 7045 7236 7408 7602
#> 1953 7798 7994 8230 8465 8694 8937 9201 9473 9710 9921 10101 10302
#> 1954 10506 10694 10929 11156 11390 11654 11956 12249 12508 12737 12940 13169
#> 1955 13411 13644 13911 14180 14450 14765 15129 15476 15788 16062 16299 16577
#> 1956 16861 17138 17455 17768 18086 18460 18873 19278 19633 19939 20210 20516
#> 1957 20831 21132 21488 21836 22191 22613 23078 23545 23949 24296 24601 24937
#> 1958 25277 25595 25957 26305 26668 27103 27594 28099 28503 28862 29172 29509
#> 1959 29869 30211 30617 31013 31433 31905 32453 33012 33475 33882 34244 34649
#> 1960 35066 35457 35876 36337 36809 37344 37966 38572 39080 39541 39931 40363
head(fcumsum(EuStockMarkets))
#> Time Series:
#> Start = c(1991, 130)
#> End = c(1991, 135)
#> Frequency = 260
#> DAX SMI CAC FTSE
#> 1991.496 1628.75 1678.1 1772.8 2443.6
#> 1991.500 3242.38 3366.6 3523.3 4903.8
#> 1991.504 4848.89 5045.2 5241.3 7352.0
#> 1991.508 6469.93 6729.3 6949.4 9822.4
#> 1991.512 8088.09 8415.9 8672.5 12307.1
#> 1991.515 9698.70 10087.5 10386.8 14773.9
fcumsum(mtcars)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 42.0 12 320.0 220 7.80 5.495 33.48 0 2 8 8
#> Datsun 710 64.8 16 428.0 313 11.65 7.815 52.09 1 3 12 9
#> Hornet 4 Drive 86.2 22 686.0 423 14.73 11.030 71.53 2 3 15 10
#> Hornet Sportabout 104.9 30 1046.0 598 17.88 14.470 88.55 2 3 18 12
#> Valiant 123.0 36 1271.0 703 20.64 17.930 108.77 3 3 21 13
#> Duster 360 137.3 44 1631.0 948 23.85 21.500 124.61 3 3 24 17
#> Merc 240D 161.7 48 1777.7 1010 27.54 24.690 144.61 4 3 28 19
#> Merc 230 184.5 52 1918.5 1105 31.46 27.840 167.51 5 3 32 21
#> Merc 280 203.7 58 2086.1 1228 35.38 31.280 185.81 6 3 36 25
#> Merc 280C 221.5 64 2253.7 1351 39.30 34.720 204.71 7 3 40 29
#> Merc 450SE 237.9 72 2529.5 1531 42.37 38.790 222.11 7 3 43 32
#> Merc 450SL 255.2 80 2805.3 1711 45.44 42.520 239.71 7 3 46 35
#> Merc 450SLC 270.4 88 3081.1 1891 48.51 46.300 257.71 7 3 49 38
#> Cadillac Fleetwood 280.8 96 3553.1 2096 51.44 51.550 275.69 7 3 52 42
#> Lincoln Continental 291.2 104 4013.1 2311 54.44 56.974 293.51 7 3 55 46
#> Chrysler Imperial 305.9 112 4453.1 2541 57.67 62.319 310.93 7 3 58 50
#> Fiat 128 338.3 116 4531.8 2607 61.75 64.519 330.40 8 4 62 51
#> Honda Civic 368.7 120 4607.5 2659 66.68 66.134 348.92 9 5 66 53
#> Toyota Corolla 402.6 124 4678.6 2724 70.90 67.969 368.82 10 6 70 54
#> Toyota Corona 424.1 128 4798.7 2821 74.60 70.434 388.83 11 6 73 55
#> Dodge Challenger 439.6 136 5116.7 2971 77.36 73.954 405.70 11 6 76 57
#> AMC Javelin 454.8 144 5420.7 3121 80.51 77.389 423.00 11 6 79 59
#> Camaro Z28 468.1 152 5770.7 3366 84.24 81.229 438.41 11 6 82 63
#> Pontiac Firebird 487.3 160 6170.7 3541 87.32 85.074 455.46 11 6 85 65
#> Fiat X1-9 514.6 164 6249.7 3607 91.40 87.009 474.36 12 7 89 66
#> Porsche 914-2 540.6 168 6370.0 3698 95.83 89.149 491.06 12 8 94 68
#> Lotus Europa 571.0 172 6465.1 3811 99.60 90.662 507.96 13 9 99 70
#> Ford Pantera L 586.8 180 6816.1 4075 103.82 93.832 522.46 13 10 104 74
#> Ferrari Dino 606.5 186 6961.1 4250 107.44 96.602 537.96 13 11 109 80
#> Maserati Bora 621.5 194 7262.1 4585 110.98 100.172 552.56 13 12 114 88
#> Volvo 142E 642.9 198 7383.1 4694 115.09 102.952 571.16 14 13 118 90
# Non-grouped but ordered
o <- order(rnorm(nrow(EuStockMarkets)))
all.equal(copyAttrib(fcumsum(EuStockMarkets[o, ], o = o)[order(o), ], EuStockMarkets),
fcumsum(EuStockMarkets))
#> [1] TRUE
## Grouped
head(with(wlddev, fcumsum(PCGDP, iso3c)))
#> [1] NA NA NA NA NA NA
## Grouped and ordered
head(with(wlddev, fcumsum(PCGDP, iso3c, year)))
#> [1] NA NA NA NA NA NA
head(with(wlddev, fcumsum(PCGDP, iso3c, year, fill = TRUE)))
#> [1] 0 0 0 0 0 0