14
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Dartで使いそうなListの統計処理集

Last updated at Posted at 2020-02-02

1.合計

void main() {
  final list = [1, 2, 3, 4, 5];
  final sum = list.reduce((a, b) => a + b);
  print(sum); // 15
}

2.最大値、最小値

import 'dart:math';

void main() {
  print([1,2,8,6].reduce(max)); // 8
  print([1,2,8,6].reduce(min)); // 1
}

3.平均

void main() {
  final list = [1, 2, 3, 4, 5];
  final mean = list.reduce((a, b) => a + b) / list.length;
  print(mean) // 3;
}

4.中央値

void main() {
  list.sort();
  final list = [1, 2, 3, 4, 5];
  final length = list.length;
  int median;

  if (length % 2 == 1) {
    median = list[(length/2 + 0.5).toInt()];
  } else {
    median = ((list[length~/2] + list[length~/2 + 1]) ~/ 2);
  }

  print(median) // 3;
}

5.標準偏差

import 'dart:math';

void main() {
  final list = [1, 2, 3, 4, 5];
  final mean = list.reduce((a, b) => a + b) / list.length;
  final standardDeviation = sqrt(
    final squaredDifference = list.map((current) {
      var difference = current - mean;
      return pow(difference, 2);
    }).reduce((previous, current) =>
      previous + current
    ) / list.length;
  );

  print(standardDeviation); // 1.58113883008
}
14
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?