File: sum2.Rd

package info (click to toggle)
r-cran-matrixstats 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,104 kB
  • sloc: ansic: 7,300; sh: 11; makefile: 2
file content (87 lines) | stat: -rw-r--r-- 2,855 bytes parent folder | download
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
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/sum2.R
\name{sum2}
\alias{sum2}
\title{Fast sum over subset of vector elements}
\usage{
sum2(x, idxs = NULL, na.rm = FALSE, mode = typeof(x), ...)
}
\arguments{
\item{x}{An NxK \code{\link[base]{matrix}} or, if \code{dim.} is specified,
an N * K \code{\link[base]{vector}}.}

\item{idxs}{A \code{\link[base]{vector}} indicating subset of elements to
operate over. If \code{\link[base]{NULL}}, no subsetting is done.}

\item{na.rm}{If \code{\link[base:logical]{TRUE}}, missing values are
excluded.}

\item{mode}{A \code{\link[base]{character}} string specifying the data type
of the return value.  Default is to use the same mode as argument \code{x},
unless it is logical when it defaults to \code{"integer"}.}

\item{...}{Not used.}
}
\value{
Returns a scalar of the data type specified by argument \code{mode}.
If \code{mode = "integer"}, then integer overflow occurs if the \emph{sum}
is outside the range of defined integer values.
Note that the intermediate sum (\code{sum(x[1:n])}) is internally
represented as a floating point value and will therefore never be outside of
the range.
If \code{mode = "integer"} and \code{typeof(x) == "double"}, then a warning
is generated.
}
\description{
Computes the sum of all or a subset of values.
}
\details{
\code{sum2(x, idxs)} gives equivalent results as \code{sum(x[idxs])}, but
is faster and more memory efficient since it avoids the actual subsetting
which requires copying of elements and garbage collection thereof.
}
\examples{
x <- 1:10
n <- length(x)

idxs <- seq(from = 1, to = n, by = 2)
s1 <- sum(x[idxs])                     # 25
s2 <- sum2(x, idxs = idxs)             # 25
stopifnot(identical(s1, s2))

idxs <- seq(from = n, to = 1, by = -2)
s1 <- sum(x[idxs])                     # 25
s2 <- sum2(x, idxs = idxs)             # 25
stopifnot(identical(s1, s2))

s1 <- sum(x)                           # 55
s2 <- sum2(x)                          # 55
stopifnot(identical(s1, s2))


# Total gives integer overflow
x <- c(.Machine$integer.max, 1L, -.Machine$integer.max)
s1 <- sum(x[1:2])                      # NA_integer_ in R (< 3.5.0)
s2 <- sum2(x[1:2])                     # NA_integer_

# Total gives integer overflow (coerce to numeric)
s1 <- sum(as.numeric(x[1:2]))          # 2147483648
s2 <- sum2(as.numeric(x[1:2]))         # 2147483648
s3 <- sum2(x[1:2], mode = "double")    # 2147483648 w/out copy
stopifnot(identical(s1, s2))
stopifnot(identical(s1, s3))

# Cumulative sum would give integer overflow but not the total
s1 <- sum(x)                           # 1L
s2 <- sum2(x)                          # 1L
stopifnot(identical(s1, s2))
}
\seealso{
\code{\link[base]{sum}}().
To efficiently average over a subset, see \code{\link{mean2}}().
}
\author{
Henrik Bengtsson
}
\keyword{internal}
\keyword{univar}