我尝试使用PowerShell计算CSV文件每列值的某些统计信息。 Measure-Object cmdlet似乎可以解决除标准偏差之外的所有需要。我在线跟踪了使用[MATH]计算标准偏差的描述,但是当我运行代码时,我在包含Pow()的行上出现以下错误:
方法调用失败,因为system.management.automation.psobject不包含名为' op_Subtraction'的方法。
这是我的代码,任何帮助都将不胜感激:
$i = 1
While ($i -le 211) {
#Set the variable to the filename with the iteration number
$filename = "c:\zMFM\z550Output\20dSummer\fixed20dSum550Output$i.csv"
#Check to see if that a file with $filename exists. If not, skip to the next iteration of $i. If so, run the code to collect the statistics for each variable and output them each to a different file
If (Test-Path $filename) {
#Calculate the Standard Deviation
#First get the average of the values in the column
$STDEVInputFile = Import-CSV $filename
#Find the average and count for column 'td'
$STDEVAVG = $STDEVInputFile | Measure-Object td -Average | Select Count, Average
$DevMath = 0
# Sum the squares of the differences between the mean and each value in the array
Foreach ($Y in $STDEVInputFile) {
$DevMath += [math]::pow(($Y - $STDEVAVG.Average), 2)
#Divide by the number of samples minus one
$STDEV = [Math]::sqrt($DevMath / ($STDEVAVG.Count-1))
}
#Calculate the basic statistics for column 'td' with the MEASURE-OBJECT cmdlet
$STATS = Import-CSV $Filename |
Measure-Object td -ave -max -min
#Append the standard deviation variable to the statistics array and add the value
$colSTDDEV = New-Object System.Data.DataColumn StdDev,([double])
$colVZA = New-Object System.Data.DataColumn VZA,([double])
$colVAZ = New-Object System.Data.DataColumn VAZ,([double])
$VZA = $Stats.VZA
$VAZ = $Stats.VAZ
$STATS.Columns.Add($colSTDDEV)
$STATS[0].StandardDev = $STDEV
$STATS.Columns.Add($colVZA)
$STATS[0].StandardDev = $VZA
$STATS.Columns.Add($colVAZ)
$STATS[0].StandardDev = $VAZ
#Export the $STATS file containing everything you need in the correct folder
Export-CSV -notype "c:\zMFM\z550Output\20dSummer\20dSum550Statistics.csv"
}
$i++
}
答案 0 :(得分:0)
$DevMath += [math]::pow(($Y - $STDEVAVG.Average), 2)
您可能需要将其替换为:
$DevMath += [math]::pow(($Y.Average - $STDEVAVG.Average), 2)
因为$Y
似乎是一个对象,而不是一个数值。