Solved

# Function to find statistical mode

Posted on 2005-05-09
189 Views
Last Modified: 2008-02-01
I am trying to create a function that will take an input of a list of numbers that are separated by spaces and output the statistical mode.  The mode is the number that occurs most frequently in the set of numbers.

I have tried doing:

\$data = array() ;                        // create empty array
\$data = explode(" ", \$datainput) ;            // brake string into array
\$mode = ???

I am not sure how to go through the data and find the value that is repeated most often though.

--
Thanks in advance
0
Question by:blainehilton
8 Comments

LVL 7

Expert Comment

foreach (array_count_vales(\$data) as \$val=>\$occurs) {
if (\$occurs > \$highestoccurs) {
\$mode = \$val;
}

}

echo "The most common (stat mode) is \$val with \$highestoccurs as the number of occurances.";
0

LVL 1

Author Comment

Okay I have:

//\$occurs = 0 ;
//\$highestoccurs = 0 ;
foreach (array_count_values(\$data) as \$val=>\$occurs) {
if (\$occurs > \$highestoccurs) {
\$mode = \$val;
}

}

and it does not work.  I tried adding the two commented lines at the top to fix PHP notices, but it still gives the wrong mode.

I tried "1 1 1 2 2 2 2 3 3 3" for instance and it says the mode is 2.
0

LVL 7

Expert Comment

The mode is 2.

Occurances of 1: 3.
Occurrances of 2: 4.
Occurances of 3: 3.

In this example, 2 is your winnder.
0

LVL 1

Author Comment

Sorry, bad example.  If I put "1 2 2 3 3 3 4 4" It says the mode is 4.  It seems no matter what it always goes with the last number entered, even if that is not the mode.

The code I am using is:

\$datainput = \$_POST["datainput"]);
\$datainput = trim(\$datainput) ;

\$data = array() ;
\$data = explode(" ", \$datainput) ;

//\$occurs = 0 ;
//\$highestoccurs = 0 ;
foreach (array_count_values(\$data) as \$val=>\$occurs) {
if (\$occurs > \$highestoccurs) {
\$mode = \$val;
}

0

LVL 7

Expert Comment

\$datainput = \$_POST["datainput"]);
\$datainput = trim(\$datainput) ;

\$data = array() ;
\$data = explode(" ", \$datainput) ;

//\$occurs = 0 ;
//\$highestoccurs = 0 ;
foreach (array_count_values(\$data) as \$val=>\$occurs) {
\$highestoccurs=0;
if (\$occurs > \$highestoccurs) {
\$mode = \$val;
}

0

LVL 1

Author Comment

It still seems to be doing the same thing.
0

LVL 1

Author Comment

I raised the points to 200 so perhaps somebody could figure this out?
0

LVL 7

Accepted Solution

Apologies for the delay. This went into the "I think that's right and I'll check it next cycle opportunity" pile.
<?
\$_POST["datainput"]="1 2 2 3 3 3 4 4";
\$datainput = \$_POST["datainput"];
\$datainput = trim(\$datainput) ;

\$data = array() ;
\$data = explode(" ", \$datainput);

\$occurs=0 ;
\$highestoccurs=0;

foreach (array_count_values(\$data) as \$val=>\$occurs) {
if (\$occurs > \$highestoccurs) {
\$mode = \$val;
\$highestoccurs=\$occurs;
}
}
echo "The statistical mode is \$mode with \$highestoccurs occurances. ";
?>
0

## Write Comment

Please enter a first name

Please enter a last name

We will never share this with anyone.

## Featured Post

### Suggested Solutions

This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

#### 761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!