1 2 3 4 5 6 7 8 9 10 11 | # This takes csv data, and identifies it by column header (bob), sums the x column, and then adds a column with x/sum x require ‘rubygems’ require ‘fastercsv’ csv="f,bob\na,0\nb,1\nc,2\nd,3\n" table = FCSV.parse(csv, :headers => true, :converters => :integer) xs = table['bob'].map{|x| x.to_i} sum = Float table['bob'].inject{|sum,i| sum += i} norm = xs.map{|x| x / sum} table['n'] = norm puts table |