-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist2matrix.rb
43 lines (37 loc) · 1.05 KB
/
list2matrix.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require "pp"
require "CSV"
# converts list data into matrix
def convert_data(inpath, outpath, index)
matrix= Hash.new()
list= CSV.read(inpath, :headers => true, :col_sep => ',')
headers = Array.new(list.headers)
headers.delete_at(list.headers.length()-1)
headers.delete_at(index)
list.each do |row|
column=row.field(index)
unless headers.include?(column)
headers << column
end
fields =row.fields
fields.delete_at(list.headers.length()-1)
fields.delete_at(index)
signature= fields.to_s
unless matrix.include?(signature)
matrix[signature]=fields
end
matrix[signature][headers.index(column)]=row.fields.last
end
# convert similar list rows into matrix rows
CSV.open(outpath, "wb", :headers => true, :col_sep => ',') do |out|
out << headers
matrix.each_value do |row|
trailers=Array.new(headers.length-row.length)
out << row + trailers
end
end
end
unless ARGV.length() == 3
pp "usage: list2matrix.rb inpath outpath index"
else
convert_data(ARGV[0],ARGV[1],ARGV[2].to_i)
end