Foreword: The Compute method in the DataTable written in this blog uses the STDEV method , but there is no STDEVP method. Sorrowful. What if the customer requires STDEVP. I searched a lot on the Internet, and one wrote what I wanted. Summarize here!
First on a StDev:
string StDevValue= table.Compute("StDev(field)", "field>0");
Probably like this.
STDEVP:
DataRow[] ddr = data.Select(dt2.Rows[j]["field"].ToString() + " is not null ");
double[] array = new double[ddr.Length];
int di = 0;
foreach (DataRow ddrs in ddr)
{
array[di] = Convert.ToDouble(ddrs[dt2.Rows[j]["field"].ToString()]);
di++;
}
double stdDev = CalculateStdDev(array);
dr[dt2.Rows[j]["field"].ToString()] = stdDev;
The above piece of code is what I need. Too lazy to organize. Take a look; you adjust the
main part yourself according to your needs
private static double CalculateStdDev(IEnumerable<double> values)
{
double ret = 0;
if (values.Count() > 0)
{
// 计算平均数
double avg = values.Average();
// 计算各数值与平均数的差值的平方,然后求和
double sum = values.Sum(d => Math.Pow(d - avg, 2));
// 除以数量,然后开方
ret = Math.Sqrt(sum / values.Count());
}
return ret;
}
Mainly this is over. Thanks everyone