proposal: add ParseFloat64(s string) (v, w float64, err error)
Created by: kortschak
We provide weighted value support for tests etc in stat, but there is no handling for textual representation of "NA" provided by stdlib. So I propose we add in either stat or floats a ParseFloat
or Parse
respectively that handles this.
Edited to incorporate suggestion below to allow a user-provided na string.
Assume in floats.
// Parse converts the string s to a float64 in v. If s equals na, w is returned as 0, otherwise 1.
func Parse(s, na string) (v, w float64, err error) {
if s == na {
return 0, 0, nil
}
v, err = strconv.ParseFloat(s, 64)
if err == nil {
w = 1
}
return v, w, err
}
This would then be used like so, e.g. single column data:
var vals, weights []float64
sc := bufio.NewScanner(r)
for sc.Scan() {
v, w, err := floats.Parse(sc.Text(), "NA")
if err != nil { ... }
vals = append(vals, v)
weights = append(weights, w)
}
err := sc.Err() ...
mean := stat.Mean(vals, weights)