github.com/gopherd/gonum@v0.0.4/floats/scalar/parse_example_test.go (about)

     1  // Copyright ©2017 The Gonum Authors. All rights reserved.
     2  // Use of this code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package scalar_test
     6  
     7  import (
     8  	"bufio"
     9  	"fmt"
    10  	"log"
    11  	"strings"
    12  
    13  	"github.com/gopherd/gonum/floats/scalar"
    14  	"github.com/gopherd/gonum/stat"
    15  )
    16  
    17  func ExampleParseWithNA() {
    18  	// Calculate the mean of a list of numbers
    19  	// ignoring missing values.
    20  	const data = `6
    21  missing
    22  4
    23  `
    24  
    25  	var vals, weights []float64
    26  	sc := bufio.NewScanner(strings.NewReader(data))
    27  	for sc.Scan() {
    28  		v, w, err := scalar.ParseWithNA(sc.Text(), "missing")
    29  		if err != nil {
    30  			log.Fatal(err)
    31  		}
    32  		vals = append(vals, v)
    33  		weights = append(weights, w)
    34  	}
    35  	err := sc.Err()
    36  	if err != nil {
    37  		log.Fatal(err)
    38  	}
    39  	fmt.Println(stat.Mean(vals, weights))
    40  
    41  	// Output:
    42  	// 5
    43  }