Calculating mean for samples 1-3 4-6 etc.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculating mean for samples 1-3 4-6 etc.
# 1  
Old 12-30-2013
Question Calculating mean for samples 1-3 4-6 etc.

Hi.
I have a LOOONG list of samples but I am not sure how to write the loop/script to calculate the mean...
I normally use awk...
...................MEAN
Sample1 25.82 40.61333
Sample1 47.6
Sample1 48.42
Sample2 54.03 54.12
Sample2 53.98
Sample2 54.35
etc.....

I would like to receive a list like this:
Sample1 40.61333
Sample2 54.12
etc...

Thanks for your help,
DNA
# 2  
Old 12-30-2013
Hi,
Please use code tag for input which you have provided in your first post.

Here you go ....
Code:
awk '{for ( i=2;i<=NF;i++){a[$1]=a[$1]+$i;b[$1]++}} END {for(j in a) { print j,a[j]/b[j]}}'  filename

# 3  
Old 12-30-2013
Quote:
Originally Posted by pravin27
Hi,
Please use code tag for input which you have provided in your first post.

Here you go ....
Code:
awk '{for ( i=2;i<=NF;i++){a[$1]=a[$1]+$i;b[$1]++}} END {for(j in a) { print j,a[j]/b[j]}}'  filename

Pravin : It's not by fields and bit faulty
Code:
$ cat sample
Sample1 25.82 
Sample1 47.6
Sample1 48.42
Sample2 54.03 
Sample2 53.98
Sample2 54.35

Code:
$ awk '{A[$1]+=$2;C[$1]++}END{for(i in A)print i,A[i]/C[i]}' sample
Sample1 40.6133
Sample2 54.12


Last edited by Akshay Hegde; 12-30-2013 at 09:41 AM..
This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 12-30-2013
If the mean has already been calculated and appears at the first line of every sample (according to given example), then :

Code:
awk 'NF==3{print $1,$3}' infile

# 5  
Old 12-31-2013
if you have Ruby
Code:
#!/usr/bin/env ruby -w
hash = Hash.new {|h,k| h[k] = [] }              # create a hash
File.open("file").each_line do|line|
  dat = line.chomp.split                       # get each line
  hash[dat[0]] += dat[1..-1].map{|i|i.to_f}   # collect similar sample data
end

hash.each_pair do |k,v|
  mean = v.inject(&:+) / v.size                 # calculate sum, then mean
  puts "#{k} mean is: #{mean}"
end

Code:
# ruby test.rb
Sample1 mean is: 40.6133325
Sample2 mean is: 54.12


Last edited by kurumi; 12-31-2013 at 01:04 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grouping and Calculating

Hi All, I want to read the input file and store the output in the Output file. I pasted the sample Input and Output file below. Help me with this. Input file ================================= ITEM1 AAAAA 1 ITEM1 BBBBB 1 ITEM1 CCCCC 1 ITEM2 AAAAA 5 ITEM2 CCCCC 4... (1 Reply)
Discussion started by: humaemo
1 Replies

2. Red Hat

EX300 - Samples Needed

May i have some samples of EX300 exam cert.? pls help ---------- Post updated at 01:12 AM ---------- Previous update was at 12:52 AM ---------- Who can share new RHCE V7 EX300 exam dumps/EX300 exam questions? ---------- Post updated at 03:48 AM ---------- Previous update was at 01:12... (1 Reply)
Discussion started by: ded325
1 Replies

3. Shell Programming and Scripting

How to print values that are greater than 0.1 in at least 80% of the samples?

input sample1 sample2 sample3 sample4 sample5 sample6 sample7 sample8 sample9 sample10 v1 0.2 0.1 0.1 0 1 2 3 4 9 10 v2 0 0 0.01 0 0 0 0 0 0 0 v3 0 0 0 0 0 ... (35 Replies)
Discussion started by: quincyjones
35 Replies

4. UNIX for Dummies Questions & Answers

Calculating average

Hi I have file like below 111,victor,48,12,36 342,Peter,54,58,30 476,Scott,25,36,48 567,Patty,74,17,95 I have written below code to calcualte avereage for every id Victor = 48+12+36/3 #!/bin/ksh /usr/xpg4/bin/awk ' BEGIN {FS=","} {sum=0; n=0;i=3 (1 Reply)
Discussion started by: stew
1 Replies

5. 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

6. UNIX for Dummies Questions & Answers

Calculating average

Hi, i have 12 float variables in a bash file and i want to calculate the average of them. Can any body help? (6 Replies)
Discussion started by: limadario
6 Replies

7. Shell Programming and Scripting

calculating in MB

hi all, have got a ksh script which tries to monitor memory usage of app servers. i do a ps -0 rss -p <PID> to get the memory size in KB but when i divide by 1024 to convert to MB i dont know how to round it up ?? thanks in advance. (3 Replies)
Discussion started by: cesarNZ
3 Replies

8. Shell Programming and Scripting

calculating a number

Hello all :) I need some help; I'm running the sp_spaceused command on various tables and saving the output to a file. So, I have an input file that has 3 rows - each row has 7 columns. I would like to 1) sort the file on the 4th column, 2) take the 4th column in the first row and add 25% to... (2 Replies)
Discussion started by: stonemonolith
2 Replies

9. UNIX for Dummies Questions & Answers

Extracting audio samples from raw file

Is there any way for me to read a raw audio file and save the samples data to a text file for further use in waveform generation? (1 Reply)
Discussion started by: sarvagya
1 Replies

10. Shell Programming and Scripting

Calculating the average

This is the cronjob ---------------------- root@a7germ:/home/paxtemp > crontab -l|grep test 57 * * * * /home/paxtemp/test_1.sh 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/paxtemp/test.sh root@a7germ:/home/paxtemp > This is the contents of test.sh script... (2 Replies)
Discussion started by: kekanap
2 Replies
Login or Register to Ask a Question