Calculating cumulative frequency


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculating cumulative frequency
# 1  
Old 12-14-2010
Calculating cumulative frequency

Hi,

I have a file containing the frequency's of an element sorted in ascending order. The file looks something like this:

#Element Frequency
1 1
2 1
3 1
4 1
5 1
6 1
7 2
8 10
9 15
10 20

What I want is to calculate and print the cumulative frequency (adding the current frequency to the sum of the previous frequency's) with the existing data. So the
output file should look like this:

#Element Frequency Cumulative Frequency
1 1 1
2 1 2
3 1 3
4 1 4
5 1 5
6 1 6
7 2 8
8 10 18
9 15 33
10 20 53

Thanks !!
# 2  
Old 12-14-2010
Code:
sum=0
while read line
do
ele=`echo "$line" | cut -f1`
freq=`echo "$line" | cut -f2`
sum=$((sum + freq))
echo "$ele $freq $sum"
done < inputfile > outputfile

R0H0N
# 3  
Old 12-14-2010
thru awk..
Code:
awk '{print $1, $2, m=$2+m}' inputfile

# 4  
Old 12-14-2010
Code:
awk '{m=(NR==1)?" Cumulative Frequency":$2+m; print $0 m}' infile

# 5  
Old 12-14-2010
Code:
awk '{$3=c+=$2}1' infile

# 6  
Old 12-14-2010
Code:
perl -lane 'print $_,$x+=$F[1]' your_file

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find out the maximum cumulative value?

I have a file has thousands of rows and each row has a number and the number can be positive or negative. I want to do the cumulative sum from the first row. How can I find out the maximum cumulative value when I do the sum work row by row. Here is the example: 4 -3 2 -3 -1 1In this case, the... (5 Replies)
Discussion started by: yuejian
5 Replies

2. Shell Programming and Scripting

awk or Bash: Cumulative average

For the data I would like to parse down and for each parsing I want a cumulative averaging, stored in an array that can be output. I.e. 546/NR = 546 (546+344)/NR=(546+344)/2 = etc. For N record input I want N values of the average (a block averaging effectively) Any... (3 Replies)
Discussion started by: chrisjorg
3 Replies

3. Shell Programming and Scripting

Calculatin cumulative elapsed time from mm:ss.ss data

Hello all, I got some time duration data like below and I want to compute the cumulative elapsed time. The data is in MM:SS.SS format. I got struck with logic on what to do when it changes from 59:59.ss to 00:00.ss. 59:59.4 59:59.6 59:59.9 00:00.1 00:00.4 00:00.6 00:00.9 I need the... (5 Replies)
Discussion started by: ks_reddy
5 Replies

4. Red Hat

Command for cumulative disk space

I wanted to know the Red Hat Linux command for cumulative disk space usage and the free space as df -h gives used and free space individually for the drives. Or, a command to check free space on the server would also be fine. I hope, my question is clear. Please revert with the reply to my... (2 Replies)
Discussion started by: RHCE
2 Replies

5. UNIX for Dummies Questions & Answers

Calculating cumulative frequency using awk

Hi, I wanted to calculate cumulative frequency distribution of my data that involves several arithmetic calls. I did things in excel but its taking me forever. this is what I want to do: var1.txt contains n observations which I have to compute for frequency which is given by 1/n and subsequently... (7 Replies)
Discussion started by: ida1215
7 Replies

6. Shell Programming and Scripting

Calculating the epoch time from standard time using awk and calculating the duration

Hi All, I have the following time stamp data in 2 columns Date TimeStamp(also with milliseconds) 05/23/2012 08:30:11.250 05/23/2012 08:30:15.500 05/23/2012 08:31.15.500 . . etc From this data I need the following output. 0.00( row1-row1 in seconds) 04.25( row2-row1 in... (5 Replies)
Discussion started by: ks_reddy
5 Replies

7. Shell Programming and Scripting

Calculating frequency of values within bins

Hi, I am working with files containing 2 columns in which i need to come up with the frequency/count of values in col. 2 falling within specifics binned values of col. 1. the contents of a sample file is shown below: 15 12.5 15 11.2 16 0.2 16 1.4 17 1.6 18 4.5 17 5.6 12 8.6 11 7.2 9 ... (13 Replies)
Discussion started by: ida1215
13 Replies

8. Shell Programming and Scripting

Help with calculating frequency of specific word in a string

Input file: #read_1 AWEAWQQRZZZQWQQWZ #read_2 ZZAQWRQTWQQQWADSADZZZ #read_3 POGZZZZZZADWRR . . Desired output file: #read_1 3 #read_1 1 #read_2 2 #read_2 3 #read_3 6 . . (3 Replies)
Discussion started by: perl_beginner
3 Replies
Login or Register to Ask a Question