4

I have a CSV data file with rows that may have lots of columns 500+ and some with a lot less. I need to transpose it so that each row becomes a column in the output file. The problem is that the rows in the original file may not all have the same number of columns so when I try the transpose method of array I get:

`transpose': element size differs (12 should be 5) (IndexError)

Is there an alternative to transpose that works with uneven array length?

1
  • 1
    Man, that's sweet that Ruby has transpose built into arrays. I've usually written a script to do it in other languages. Commented Oct 30, 2008 at 16:25

2 Answers 2

9

I would insert nulls to fill the holes in your matrix, something such as:

a = [[1, 2, 3], [3, 4]]

# This would throw the error you're talking about
# a.transpose

# Largest row
size = a.max { |r1, r2| r1.size <=> r2.size }.size

# Enlarge matrix inserting nils as needed
a.each { |r| r[size - 1] ||= nil }

# So now a == [[1, 2, 3], [3, 4, nil]]
aa = a.transpose

# aa == [[1, 3], [2, 4], [3, nil]]
Sign up to request clarification or add additional context in comments.

Comments

2
# Intitial CSV table data
csv_data = [ [1,2,3,4,5], [10,20,30,40], [100,200] ]

# Finding max length of rows
row_length = csv_data.map(&:length).max

# Inserting nil to the end of each row
csv_data.map do |row|
  (row_length - row.length).times { row.insert(-1, nil) }
end

# Let's check
csv_data
# => [[1, 2, 3, 4, 5], [10, 20, 30, 40, nil], [100, 200, nil, nil, nil]]

# Transposing...
transposed_csv_data = csv_data.transpose

# Hooray!
# => [[1, 10, 100], [2, 20, 200], [3, 30, nil], [4, 40, nil], [5, nil, nil]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.