# Storing and manipulating columns of data

**URL:** <https://www.ruby-forum.com/t/storing-and-manipulating-columns-of-data/108223>\
**Category:** Ruby\
**Created:** [June 30, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/storing-and-manipulating-columns-of-data/108223 "2007-06-30T12:39:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 30, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/storing-and-manipulating-columns-of-data/108223/1 "2007-06-30T12:39:15Z")

</div>

Hi,

Being used to deal with Octave / Matlab, I’m a bit confused by Ruby  
structures (arrays, hashes, etc). I have this data file “data.dat”  
with 3 columns:

> 6.199000 1.001000e+00 4.720000e-05  
> 8.265000 9.990000e-01 1.210000e-04

I would like to think of the columns as arrays “a”, “b”, “c”, execute  
a loop along their indices, pick the values i want, and do something  
with them. What would be a sensible data structure for this?  
I can read this datafile line by line using IO:readlines(“data.dat”),  
but I cannot find a way to refer to one column in the array of  
strings generated.

Best regards,

baptiste

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 30, 2007, 12:52pm UTC](https://www.ruby-forum.com/t/storing-and-manipulating-columns-of-data/108223/2 "2007-06-30T12:52:17Z")

</div>

baptiste Auguié wrote:

> > 2.480000 1.001000e+00 1.520000e-05  
> > loop along their indices, pick the values i want, and do something with  
> > them. What would be a sensible data structure for this?  
> > I can read this datafile line by line using IO:readlines(“data.dat”),  
> > but I cannot find a way to refer to one column in the array of strings  
> > generated.  
> > In native Ruby, the only way to do that would be:

arr = [[0,1],[2,3],[4,5]]  
arr.map{|a| a[0]}

# =\> [0,2,4]

If you can install rb-gsl (and it’s probably a good idea if you’re  
expecting to do matlab/octavey things), then there’s the  
GSL::Matrix#column method which (as I understand it, not being a heavy  
GSL user) actually gives a reference into the original matrix, rather  
than copying to a new one as Array#map does.

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 30, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/storing-and-manipulating-columns-of-data/108223/3 "2007-06-30T14:01:21Z")

</div>

baptiste Auguié wrote:

> but I cannot find a way to refer to one column in the array of strings  
> generated.

You can use String#split to split each line into an array of 3 numbers.  
If you want to use the array values as numbers, you’ll need to use  
String#to\_f as well to convert the strings to numeric values.

ri String#split  
ri String#to\_f

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 30, 2007, 2:03pm UTC](https://www.ruby-forum.com/t/storing-and-manipulating-columns-of-data/108223/4 "2007-06-30T14:03:21Z")

</div>

On Jun 30, 2007, at 6:37 AM, baptiste Auguié wrote:

> > 3.099000 1.003000e+00 3.570000e-05  
> > I can read this datafile line by line using IO:readlines  
> > (“data.dat”), but I cannot find a way to refer to one column in the  
> > array of strings generated.

Maybe something like this is what you’re looking for:

`
data = DATA.readlines
rows = data.map do |row|
    row.split(/\s+/).map { |elt| elt.to_f }
end
a, b, c = rows.transpose
p a, b, c
__END__
1.240000 9.990000e-01 1.290000e-06
1.550000 1.000000e+00 2.920000e-06
2.066000 1.002000e+00 8.360000e-06
2.480000 1.001000e+00 1.520000e-05
` [1.24, 1.55, 2.066, 2.48] [0.999, 1.0, 1.002, 1.001] [1.29e-06, 2.92e-06, 8.36e-06, 1.52e-05]

Regards, Morton

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 30, 2007, 2:43pm UTC](https://www.ruby-forum.com/t/storing-and-manipulating-columns-of-data/108223/5 "2007-06-30T14:43:22Z")

</div>

Dear Baptiste,

I’d also recommend rb-gsl, but if you have trouble  
installing from source, you can also use the Matrix  
class from Ruby:

[http://www.ruby-doc.org/stdlib/libdoc/matrix/rdoc/classes/Matrix.html](http://www.ruby-doc.org/stdlib/libdoc/matrix/rdoc/classes/Matrix.html)

require “matrix”

a=Matrix.columns([[25, 93], [-1, 66]])  
=\> 25 -1  
93 66

a.transpose  
=\> 25 93  
-1 66

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![baptiste\_AuguiSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/71c47a/32.png) [@baptiste\_AuguiSSSS](https://www.ruby-forum.com/u/baptiste_AuguiSSSS)\
**Post date:** [June 30, 2007, 6:46pm UTC](https://www.ruby-forum.com/t/storing-and-manipulating-columns-of-data/108223/6 "2007-06-30T18:46:45Z")

</div>

Thanks everyone for your suggestions, it’s good to know different  
options. This community is great!

Thanks,

baptiste
