Skip to content

Compute weighted mean from a dataframe? #88

@floswald

Description

@floswald

i can only compute a weighted mean from a dataframe object after converting the column x to a float array. I don't understand why, since DataArray <: AbstractArray is true?

julia> df = DataFrame(x = rand(3),w=rand(3))
3x2 DataFrame
| Row | x        | w         |
|-----|----------|-----------|
| 1   | 0.226874 | 0.266516  |
| 2   | 0.346735 | 0.57437   |
| 3   | 0.232796 | 0.0512731 |

julia> mean(df[:x],WeightVec(df[:w]))
ERROR: `start` has no method matching start(::WeightVec{Float64,DataArray{Float64,1}})
 in reduced_dims at reducedim.jl:17
 in mean at /Users/florianoswald/.julia/v0.3/DataArrays/src/reducedim.jl:335

julia> mean(array(df[:x]),WeightVec(df[:w]))
0.30438070685782537

julia> methods(mean)
...
mean(v::AbstractArray{T,N},w::WeightVec{W<:Real,Vec<:AbstractArray{T<:Real,1}}) at /Users/florianoswald/.julia/v0.3/StatsBase/src/weights.jl:234
...

julia> typeof(df[:x])
DataArray{Float64,1} (constructor with 1 method)

julia> DataArray <: AbstractArray
true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions