MODE function in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting MODE function in awk
# 1  
Old 10-07-2008
MODE function in awk

Hello,

Can someone pls help me with some statistical calculation in awk

In excel there is a statistical function called "Mode".

How Mode works:

MODE returns the most frequently occurring, or repetitive, value in array or range.


Eg if we have 5 numbers in 5 different columns ( 32,45,56,32,32)
The MODE(32,45,56,32,32) = 32

If we had (32,45,56,30,92) , then the MODE would return "#N/A"


How can I Code the "Mode" function in awk?

I tried this code below, but it didn't work.

thanks

Code:
 
#!/bin/sh
find . -name '*.txt' | while read line
do
gawk -F '\t' '$2== "A" {print $1"\t" $3"\t" Mode($7,$8,$9,$10)"\t" }' $line
done >out.txt

# 2  
Old 10-07-2008
#!/bin/sh
find . -name "*.txt" | while read line
do
gawk -F '\t' '$2== "A" {print $1"\t" $3"\t" Mode($7,$8,$9,$10)"\t" }' $line
done >out.txt
# 3  
Old 10-07-2008
You may try something like this:
(use should use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
func mode(list, sep,  n, a, i, h, m, v) {
  sep = sep ? sep : FS
  n = split(list, a, sep)
  while (++i <= n) {
    if (++h[a[i]] > m) {
      m = h[a[i]]
      v = a[i]
    }
  }
  return m > 1 ? v : "#N/A"
}

For example:

Code:
% cat file
32,45,56,32,32
32,45,56,30,92

% awk -F, '{ print mode($0) }
func mode(list, sep,  n, a, i, h, m, v) {
  sep = sep ? sep : FS
  n = split(list, a, sep)
  while (++i<= n) {
    if (++h[a[i]] > m) {
      m = h[a[i]]
      v = a[i]
    }
  }
  return m > 1 ? v : "#N/A"
}' file
32
#N/A

What should the function return for an input like the one below?

Code:
1,1,2,2

# 4  
Old 10-07-2008
radulov - it is bimodal, really. It should return a set: (1,2). I don't know what Excel does, just what statistics does. Mode is often used for data like 'birthdays of people in the room' or 'last names of people in a town' - data unlike numbers or numeric data with no real inherent meaning, just magnitude. Football (soccer) scores qualify in that category.
# 5  
Old 10-07-2008
Another way...
Code:
awk -F\, '{
   for (i=1; i<=NF; ++i)
       if (max <= ++x[$i])
          max = x[$i]
} END {
   for (i in x)
       if (x[i] == max)
          print i
}' file

# 6  
Old 10-07-2008
shamrock's code is a lot closer to what's needed, except the (#/N/A) non-modal part.
# 7  
Old 10-07-2008
Question non-modal

Quote:
Originally Posted by jim mcnamara
shamrock's code is a lot closer to what's needed, except the (#/N/A) non-modal part.
As I am not very statistically inclined could you explain what non-modal means?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. HP-UX

From a C++ application how to find if a hpux host is in standard mode or trusted mode

is there a way for my C++ application to find out which mode the hpux OS is running in? standard mode or trusted mode. (3 Replies)
Discussion started by: einsteinBrain
3 Replies

3. Shell Programming and Scripting

awk function

hi, I have used awk command to delimit my variable. But this that not worked. Could you please let me know what need to be changed in my awk command Input: home/unix>cat test.txt DAILY.JOB CHENNAI,8388 DAILY.JOB BANGLORE,3848 DAILY.JOB TRICHY,9489 DAILY.JOB TIRUPUR,8409 code ... (9 Replies)
Discussion started by: arun888
9 Replies

4. UNIX for Dummies Questions & Answers

A Freebie Text Mode Plot, (Drawing), Function...

This is probably common knowledge to the big guns but this is for newbies... This is a demo using simple plot function using bash. In a default Terminal... It clears the window, sets the cursor to the top left hand corner, draws a triangle inside the window, writes a string inside the... (0 Replies)
Discussion started by: wisecracker
0 Replies

5. Shell Programming and Scripting

awk function

Hi all, I need to have informations in a URL : https://www.autolib.eu/stations/ Valors I need are in bold: {"charging_status": "nonexistent", "rental_status": "future", "subscription_status": "nonexistent", "station_id": 791, "address": "10 rue de Rome, 93110 Rosny-sous-Bois", "lat":... (3 Replies)
Discussion started by: roulitto
3 Replies

6. Solaris

DNS service is in maintenance mode. How to bring it back to online mode?

:confused: when i tried to look the status of DNS-client, it is in maintenance mode..... Please tell me how to bring it back to online mode...PLEASE TELL ME STEP BY STEP.... PLEASE... :wall: (2 Replies)
Discussion started by: vamshigvk475
2 Replies

7. UNIX for Advanced & Expert Users

What is the difference between single line mode and multiline mode in Regular expressions?

Hi All, Can please let me know what is the difference between the single line mode and multi line mode in regular expresions? Thanks, Chidhambaram B (3 Replies)
Discussion started by: chidhu.anu
3 Replies

8. Programming

which function copies data from user to kernel mode

when transitionaning from user to kernel mode which function copies data from user mode buffer to kernel mode? (5 Replies)
Discussion started by: rupeshkp728
5 Replies

9. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

10. SuSE

Convet Linux OS from text mode to graphic mode

Hi All, I used to have my suse linux(VM) server in graphic mode but not anymore since morning. I cant rolback since i loose somuch work. Any idea how to it back to normal. Thanks (6 Replies)
Discussion started by: s_linux
6 Replies
Login or Register to Ask a Question