This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:create:functions:valuemean [21.01.2015 12:30] – [valueMean()] alexander.ritter | en:create:functions:valuemean [14.03.2022 12:02] (current) – walliser | ||
---|---|---|---|
Line 11: | Line 11: | ||
* // | * // | ||
* //items// -- A list (string or array) of items | * //items// -- A list (string or array) of items | ||
- | * // | + | * // |
- | **Note** This works in the same way as '' | + | **Note** This works in the same way as '' |
- | ===== Example ===== | + | ===== Example |
In the following example, question " | In the following example, question " | ||
Line 27: | Line 27: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | |||
+ | ===== Example items ===== | ||
+ | |||
+ | The following PHP code calculates the mean of the items 2, 4, 6, 8 and 10 in question AB02. | ||
+ | |||
+ | |||
+ | <code php> | ||
+ | valueMean(' | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Example variables ===== | ||
+ | |||
+ | The following PHP code calculates the mean of the variables AB03_01, AB03_02, BB01_02 and BB01_04. | ||
+ | |||
+ | <code php> | ||
+ | valueMean([' | ||
+ | </ | ||
+ | |||
+ | |||
+ | Whether a [[: | ||
+ | |||
+ | <code php> | ||
+ | valueMean([ | ||
+ | ' | ||
+ | ' | ||
+ | ]) | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Example weighting ===== | ||
+ | |||
+ | It is not possible to calculate a weighted average using " | ||
+ | |||
+ | <code php> | ||
+ | $weights = [ | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ]; | ||
+ | $sumValue = 0; | ||
+ | $sumWeight = 0; | ||
+ | |||
+ | foreach ($weights as $varID => $weight) { | ||
+ | $val = (float)value($varID); | ||
+ | // Exclude missing data (≤0) | ||
+ | if ($val > 0) { | ||
+ | $sumValue+= $val * $weight; | ||
+ | $sumWeight+= $weight; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Mean = sum divided by count | ||
+ | if ($sumWeight == 0) { | ||
+ | $mean = -1; // No data | ||
+ | } else { | ||
+ | $mean = $sumValue / $sumWeight; | ||
+ | } | ||
+ | </ | ||
+ |