Skip to content
Advertisement

Is there a way to get cumulative percentages in PHP?

I want to run cumulative percentages. I can do it for my array in R as follows:

df<- data.frame( x= c(6,8,9,8,9,8,5,4,4,9))


df %>% 
    count(x) %>% 
    mutate(cum=cumsum(n)/sum(n)) %>%
    complete(x = seq(min(x), max(x)), fill = list(n = 0)) %>%
    fill(cum)

and I get the results:

A tibble: 6 x 3
      x     n   cum
  <dbl> <dbl> <dbl>
1     4     2   0.2
2     5     1   0.3
3     6     1   0.4
4     7     0   0.4
5     8     3   0.7
6     9     3   1  

As you can see, I get the value of 7 as well using this code.

I want to get the same output using PHP.

I have written this code so far to get frequency, but as you can see the value 7 has not appeared.

I want to get cumulative percentages (cum) which I was unable to do. The code that I have used is:

<?php
$array = array(6,8,9,8,9,8,5,4,4,9);
function Counting($array){
    return(array_count_values($array));    
}

print_r(Counting($array)); 

?>

Advertisement

Answer

array_count_values would give you just the frequency of elements. To find out cumulative, you would have to do the addition yourself.

<?php 
$array = array(6,8,9,8,9,8,5,4,4,9);
function Counting($array){ 
    $freq_data = array_count_values($array);
    $unique_elements = array_map('intval',array_keys($freq_data));
    sort($unique_elements);
    $percentage_data = [];
    $total = count($array);
    foreach($unique_elements as $index => $element){
        $current_percentage = $freq_data[$element] / $total;
        $percentage_data[$element] = $index > 0 ?  $percentage_data[$prev_element] +  $current_percentage : $current_percentage;
        $prev_element = $element;
    }
    return $percentage_data;
} 

print_r(Counting($array)); 

Demo: https://3v4l.org/vodsf


Update:

Since you need all the missing elements between min and max elements, you can just loop over the frequency data and have an isset check to gather the cumulatives for missing as well as non missing.

<?php 
$array = array(6,8,9,8,9,8,5,4,4,9);
function Counting($array){ 
    $freq_data = array_count_values($array);
    $unique_elements = array_map('intval',array_keys($freq_data));

    $min_element = min($unique_elements);
    $max_element = max($unique_elements);

    $percentage_data = [];
    $total = count($array);

    for($i = $min_element; $i <= $max_element ; ++$i){
        $current_percentage = isset($freq_data[$i]) ? ($freq_data[$i] / $total) : 0;
        $percentage_data[$i] = $i > $min_element ?  $percentage_data[$i-1] +  $current_percentage : $current_percentage;
    }

    uksort($percentage_data,function($a,$b){
        return strnatcmp($a,$b);
    });// just to be sure that associative hash keys are in ascending order

    return $percentage_data;
} 

print_r(Counting($array));

Demo: https://3v4l.org/41g2n

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement