# Sample() from a data vector with NA values

**URL:** https://discourse.julialang.org/t/sample-from-a-data-vector-with-na-values/2482
**Category:** General Usage
**Tags:** statistics
**Created:** [March 6, 2017, 1:36am UTC](https://discourse.julialang.org/t/sample-from-a-data-vector-with-na-values/2482 "2017-03-06T01:36:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [March 6, 2017, 1:36am UTC](https://discourse.julialang.org/t/sample-from-a-data-vector-with-na-values/2482/1 "2017-03-06T01:36:15Z")

</div>

Is there a way to use `sample()` function from a `DataArray` in a way that deals gracefully with `NA` values?

What I mean is that if I do `sample(a)`, it works fine - sometimes returning a `Float64`, and sometimes returning NA. But when I add a number (eg `sample(a, 5)`, it works if all the samples are `Float64`s or if they’re all `NA`s, but if it gets a mix (i assume), I get:

```julia
MethodError: cannot `convert` an object of type DataArrays.NAtype to an object of type Float64

```

Is there anyway around this (so that my sample can have both `Float64` and `NA` values)?

EDIT: whole error message:

```julia
MethodError: Cannot `convert` an object of type DataArrays.NAtype to an object of type Float64
This may have arisen from a call to the constructor Float64(...),
since type constructors fall back to convert methods.
 in setindex!(::Array{Float64,1}, ::DataArrays.NAtype, ::Int64) at array.jl:415
 in direct_sample!(::DataArrays.DataArray{Float64,1}, ::Array{Float64,1}) at sampling.jl:35
 in #sample!#66(::Bool, ::Bool, ::Function, ::DataArrays.DataArray{Float64,1}, ::Array{Float64,1}) at sampling.jl:280
 in (::StatsBase.#kw##sample!)(::Array{Any,1}, ::StatsBase.#sample!, ::DataArrays.DataArray{Float64,1}, ::Array{Float64,1}) at <missing>:0
 in sample(::DataArrays.DataArray{Float64,1}, ::Int64) at sampling.jl:318
 in include_string(::String, ::String) at loading.jl:441
 in include_string(::String, ::String, ::Int64) at eval.jl:28
 in include_string(::Module, ::String, ::String, ::Int64, ::Vararg{Int64,N}) at eval.jl:32
 in (::Atom.##53#56{String,Int64,String})() at eval.jl:40
 in withpath(::Atom.##53#56{String,Int64,String}, ::String) at utils.jl:30
 in withpath(::Function, ::String) at eval.jl:46
 in macro expansion at eval.jl:57 [inlined]
 in (::Atom.##52#55{Dict{String,Any}})() at task.jl:60

```

---

<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: [March 6, 2017, 2:19am UTC](https://discourse.julialang.org/t/sample-from-a-data-vector-with-na-values/2482/2 "2017-03-06T02:19:49Z")

</div>

You didn’t report the whole error message. To which type is it trying to `convert`?  
What should `sample()` return if it hits an `NA`?

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [March 6, 2017, 7:54am UTC](https://discourse.julialang.org/t/sample-from-a-data-vector-with-na-values/2482/3 "2017-03-06T07:54:57Z")

</div>

I don’t think so, but you can do it with the new NullableArrays data format.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [March 6, 2017, 8:20am UTC](https://discourse.julialang.org/t/sample-from-a-data-vector-with-na-values/2482/4 "2017-03-06T08:20:45Z")

</div>

`sample` constructs an output `Vector{T}` using the `eltype` of the input. You can use the in-place mutating `sample!` instead, and provide your own `DataArray`:

```julia
julia> sample!(a, similar(a, 5)) # Note the argument order; output comes second here
5-element DataArrays.DataArray{Int64,1}:
…

```

Perhaps changing `sample` to use `similar` internally would be generally useful.

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [March 6, 2017, 1:31pm UTC](https://discourse.julialang.org/t/sample-from-a-data-vector-with-na-values/2482/5 "2017-03-06T13:31:11Z")

</div>

> You can use the in-place mutating sample! instead, and provide your own DataArray

This works perfectly, thanks!

I agree that this would be useful generally, but I’m not sure what other implications this has. Perhaps just a note in the docs mentioning this work-around would be sufficient - this strikes me as something that could trip up more than just me. It was particularly odd that it worked sometimes (if none of the samples were `NA` )
