This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:create:functions:valuemean [24.01.2015 17:15] – [valueMean()] alexander.ritter | en:create:functions:valuemean [14.03.2022 12:02] (current) – walliser | ||
|---|---|---|---|
| Line 16: | Line 16: | ||
| - | ===== 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; | ||
| + | } | ||
| + | </ | ||
| + | |||