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

Hi,
Please use code tag for input which you have provided in your first post.

Here you go ....

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

$ cat sample
Sample1 25.82 
Sample1 47.6
Sample1 48.42
Sample2 54.03 
Sample2 53.98
Sample2 54.35
$ awk '{A[$1]+=$2;C[$1]++}END{for(i in A)print i,A/C}' sample
Sample1 40.6133
Sample2 54.12
1 Like

If the mean has already been calculated and appears at the first line of every sample (according to given example), then :

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

if you have Ruby

#!/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

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