# Sum of Any - Why sum of empty set is not zero?

**URL:** <https://discourse.julialang.org/t/sum-of-any-why-sum-of-empty-set-is-not-zero/50706>\
**Category:** General Usage\
**Created:** [November 24, 2020, 7:10pm UTC](https://discourse.julialang.org/t/sum-of-any-why-sum-of-empty-set-is-not-zero/50706 "2020-11-24T19:10:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [November 24, 2020, 7:10pm UTC](https://discourse.julialang.org/t/sum-of-any-why-sum-of-empty-set-is-not-zero/50706/1 "2020-11-24T19:10:41Z")

</div>

Why sum of empty set is not zero ?

```julia
julia> eltype(dane)
Any
julia> sum(dane[:,9])
6.54132454e8
julia> dane[(mies20) .& (dane[:,7].==dealerzy[1]),9]
1-element Array{Any,1}:
 2032.0
julia> dane[(mies20) .& (dane[:,7].==dealerzy[2]),9]
Any[]

julia> sum(dane[(mies20) .& (dane[:,7].==dealerzy[2]),9])
ERROR: MethodError: no method matching zero(::Type{Any})
Closest candidates are:
  zero(::Type{Union{Missing, T}}) where T at missing.jl:104
  zero(::Type{Measures.Length{:mm,Float64}}) at C:\Users\PC\.julia\packa
  zero(::Type{Measures.Length{:pct,Float64}}) at C:\Users\PC\.julia\pack
  ...

julia> isreal(dane[(mies20) .& (dane[:,7].==dealerzy[2]),9])
true

```

Paul

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 24, 2020, 7:17pm UTC](https://discourse.julialang.org/t/sum-of-any-why-sum-of-empty-set-is-not-zero/50706/2 "2020-11-24T19:17:54Z")

</div>

You can have sums of lots of objects, not just numbers. There is no reason to assume that if a vector is of type `Any` the default “zero” value should be `0`.

---

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [November 24, 2020, 7:25pm UTC](https://discourse.julialang.org/t/sum-of-any-why-sum-of-empty-set-is-not-zero/50706/3 "2020-11-24T19:25:08Z")

</div>

> [@pdeffebach](#):
>
> You can have sums of lots of objects, not just numbers. There is no reason to assume that if a vector is of type `Any` the default “zero” value should be `0` .

Is it possible to set a Float type for one column of the matrix when the others are Any or String ?  
Paul

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 24, 2020, 7:31pm UTC](https://discourse.julialang.org/t/sum-of-any-why-sum-of-empty-set-is-not-zero/50706/4 "2020-11-24T19:31:33Z")

</div>

> [@programista](#):
>
> Is it possible to set a Float type for one column of the matrix when the others are Any or String ?

You can do `reduce(+, a, init=0.0)` instead of `sum(a)` to force it to accumulate as floating point. (In Julia 1.6 you will be able to do `sum(a, init=0.0)` also.)

```julia
julia> reduce(+, Any[], init=0.0)
0.0

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [November 24, 2020, 7:54pm UTC](https://discourse.julialang.org/t/sum-of-any-why-sum-of-empty-set-is-not-zero/50706/5 "2020-11-24T19:54:12Z")

</div>

That sounds like you want a `DataFrame` from the `DataFrames.jl` package.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [November 24, 2020, 7:57pm UTC](https://discourse.julialang.org/t/sum-of-any-why-sum-of-empty-set-is-not-zero/50706/6 "2020-11-24T19:57:19Z")

</div>

Or possibly a vector of structs
