Home

rowSums

rowSums is a function in the R programming language that computes the sum of each row in a matrix or array-like object. It returns a numeric vector with one element for every row, making it useful for creating row totals or per-row aggregates in data analysis. The function is part of base R and is commonly used with numeric data.

The typical usage is rowSums(x, na.rm = FALSE, dims = 1). Here, x should be an object with

Notes on data types and preparation: rowSums operates on numeric data. When used with data frames, it

Examples: For a 2x3 matrix m created by filling values 1 through 6, rowSums(m) returns c(6, 15).

See also: rowMeans, apply, colSums. Similar functionality exists in other languages, such as NumPy’s sum with

at
least
two
dimensions,
such
as
a
matrix
or
a
two-dimensional
array.
The
na.rm
argument
controls
how
missing
values
are
handled:
if
na.rm
is
FALSE
(the
default),
any
row
containing
an
NA
yields
NA
in
the
result;
if
na.rm
is
TRUE,
NA
values
are
ignored
when
computing
the
row
sums.
The
dims
parameter
is
used
for
higher-dimensional
arrays
and
is
less
often
needed
for
simple
matrices.
is
common
to
select
numeric
columns
or
convert
the
frame
to
a
numeric
matrix
first
to
avoid
unintended
coercion
of
non-numeric
columns.
If
m
contains
missing
values,
such
as
m
<-
matrix(c(1,
NA,
3,
4,
5,
NA),
nrow
=
2,
byrow
=
TRUE),
rowSums(m,
na.rm
=
TRUE)
yields
c(4,
9).
With
a
data
frame
like
df
<-
data.frame(a
=
c(1,
2),
b
=
c(3,
4)),
rowSums(df)
returns
c(4,
6).
axis=1
in
Python
and
pandas’
sum(axis=1).