Help with calculate median, first quartile, second quartile and third quartile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with calculate median, first quartile, second quartile and third quartile
# 1  
Old 06-06-2011
Help with calculate median, first quartile, second quartile and third quartile

Input file:
Code:
21.08
21.06
20.98
20.65
18.52
16.34
13.58
12.2
10.66
10.22
9.8
8.6
7.4
3.9
3.5

Desired output file:
Code:
8.6
12.2
20.65

I wanna to calculate 25th percentile/first quartile (Q1), 50th percentile/second quartile (Q2)/median, 75th percentile/third quartile (Q3) from the input file.
It is also acceptable to calculate Q1,Q2,Q3 separately.
Based on my understanding of quartile, 8.6 is the Q1, 12.2 is Q2, 20.65 is Q3.

The way I calculate Q1,Q2,Q3 is based on the following criteria:
sort the figure from smallest to largest
The median is the (15 + 1) ÷ 2 = 8th value = 12.2;

I take the 8th value as the center. Split the data into lower and upper part

In lower part, the lower quartile (Q1) is the (7 + 1) ÷ 2 = 4th value = 8.6;
In upper part, the upper quartile is the 3 (7 + 1) ÷ 2 = 12th value = 20.65.

Thanks in advance.

Last edited by perl_beginner; 06-06-2011 at 03:09 AM..
# 2  
Old 06-06-2011
You need provide the formula, how to get 8.6?
# 3  
Old 06-06-2011
Hi rdcwayx,

I just edit my question and include the way how I calculate median, first and third quartile manually.
Thanks for your advice.
# 4  
Old 06-06-2011
In upper part, the upper quartile is the 3 (7 + 1) ÷ 2 = 4th value = 20.65.

should be

In upper part, the upper quartile is the 3 (7 + 1) ÷ 2 = 12th value = 20.65.

right ?
# 5  
Old 06-06-2011
Hi itkamaraj,
Thanks for reminding Smilie
You're right.
Do you have any idea to solve this problem by using command?
# 6  
Old 06-06-2011
you can write a shell script.

i dont know, whethere it is achievable in one line command
# 7  
Old 06-06-2011
Try this

Code:
user@tioman> (/home/user) $ cat test.sh
#!/bin/bash


filename="tmp.txt"

sort -n $1 >$filename


rows=`wc -l $filename|cut -d' ' -f1`
q2=`echo "($rows+1)/2" |bc`

q1=`echo "$q2 / 2"|bc`

q3=`echo "3 * $q1" |bc
`
echo  "Q1=  " `head -$q1 $filename|tail -1`

echo  "Q2= "`head -$q2 $filename|tail -1`

echo  "Q3= "`head -$q3 $filename|tail -1`

user@tioman> (/home/user) $ ./test.sh test.txt
Q1=   12.2
Q2= 8.6
Q3= 20.65
user@tioman> (/home/user) $


Last edited by kumaran_5555; 06-06-2011 at 04:02 AM.. Reason: Edited the q1,q2,q3
This User Gave Thanks to kumaran_5555 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Median calculator based on id match

I am trying to calculate the median of a column of numbers if they match an ID type on a different column. The input file has 3 columns. The column that has the ID is column 1 and the column with the values I'd like to find the median for is column 3. The file does not need to be sorted. What I... (9 Replies)
Discussion started by: verse123
9 Replies

2. Shell Programming and Scripting

Median and max of duplicate rows

Hi all, plz help me with this, I want to to extract the duplicate rows (column 1) in a file which at least repeat 4 times. then I want to summarize them by getting the max , mean, median and min. The file is sorted by column 1, all the repeated rows appear together. If number of elements is... (5 Replies)
Discussion started by: ritakadm
5 Replies

3. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

4. UNIX for Dummies Questions & Answers

Help with Median

Hi, I know this question has been asked before, but I'd like to ask for some explanation rather than solution regarding finding median in unix. I've got a simple one column file with over 2 million numbers and I need to find its median. The file looks like this: 0.123 0.235 0.890 0.000 etc... (11 Replies)
Discussion started by: zajtat
11 Replies

5. UNIX for Dummies Questions & Answers

Calculate the Median, first quartile and third quartile using AWK

Hi all, I have a data range as follow: 28 33 42 12 9 68 81 55 6 47 Since I want to create Box & Whisker Plot, I need to calculate the median, first quartile and third quartile of above data using AWK.( so far I can only writing a code for determine smallest value & largest value... (4 Replies)
Discussion started by: nica
4 Replies

6. Shell Programming and Scripting

awk to median

hi! i have a file like the attachement. you can see on the last column, there is a marker from 1 to 64 for each time. I'd like to have the median for each marker: i want to get a median every 128 values the result is : for an hour and marker x, i have the median value thank you for... (5 Replies)
Discussion started by: riderman
5 Replies
Login or Register to Ask a Question