Search code examples
arraysrubytransposefastercsv

Converting uneven rows to columns with FasterCSV


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?


Solution

  • 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]]