3

I have a vector layer with an attribut called "value". To make the explanation easier, we can say that "value" goes between 0 and 100.

I can't find a way the calculate a new attribute called "effect" in expression panel for something like that:

CASE
WHEN "value" is under the 20% lowest possible value THEN 'Very low'
WHEN "value" is between 20-40% lowest possible value THEN 'Low'
WHEN "value" is between 40-60% lowest possible value THEN 'Medium'
WHEN "value" is between 60-80% lowest possible value THEN 'High'
ELSE 'Very high'
END

So as you can see, I have a problem to find a way to do "value" is between x-y% lowest possible value.

1

2 Answers 2

7

Given that Percentile = (Number of Values Below “x” / Total Number of Values), how about:

with_variable(
'percentile',
array_find(array_agg("val",order_by:="val"),"val") / array_length(array_agg("val")),
    
    CASE
    WHEN @percentile < 0.2 THEN 'Very low'
    WHEN @percentile < 0.4 THEN 'Low'
    WHEN @percentile < 0.6 THEN 'Medium'
    WHEN @percentile < 0.8 THEN 'High'
    WHEN @percentile <= 1 THEN 'Very High'
    ELSE 'Undefined'
    END
)
4
  • Not sure if I understood correctly though
    – MrXsquared
    Commented Jan 18, 2023 at 8:43
  • 1
    Quantiles are the idea of dividing the range into classes so that each class has the same number of features. Commented Jan 18, 2023 at 8:59
  • Nicely done, I failed finding how to do that, I was trying with count($id,"value"< attribute( $currentfeature, "value" ))/(count($id)
    – Kasper
    Commented Jan 18, 2023 at 9:49
  • I love this solution, really useful, thanks
    – geotyr
    Commented Jan 18, 2023 at 12:26
2

I would do it that other way...which I think is not the same statistically..these are not percentiles

CASE
WHEN "value" < minimum("value")+ 0.2*(maximum("value")-minimum("value")) THEN 'Very low'
WHEN "value" > minimum("value")+ 0.2*(maximum("value")-minimum("value")) AND "value" < minimum("value")+ 0.4*(maximum("value")-minimum("value"))THEN 'Low'
WHEN "value" > minimum("value")+ 0.4*(maximum("value")-minimum("value")) AND "value" < minimum("value")+ 0.6*(maximum("value")-minimum("value"))THEN 'Medium'
WHEN "value" > minimum("value")+ 0.6*(maximum("value")-minimum("value")) AND "value" < minimum("value")+ 0.8*(maximum("value")-minimum("value"))THEN 'High'
ELSE 'Very high'
END
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.