
R Language Fundamentals 15
that apply to zero length vectors for those computations. Here we describe
their behavior in other settings.
Functions such as sum and prod take as input one or more vectors and pro-
duce a value of length one. It is helpful if simple rules, such as
sum(c(x,y)) = sum(x) + sum(y), hold. Similarly for prod we expect
prod(c(x,y)) = prod(x)*prod(y). For these to hold, we require that the
sum of a zero length vector be zero and that the product of a zero length
vector be one.
> sum(numeric())
[1] 0
> prod(numeric())
[1] 1
For other mathematical functions, such as gamma or log, the same logic
suggests that these functions should return ...