Demostrate basic creation of Vector and DataFrame¶
In [1]:
require 'daru'
samples = 1000
# We set lazy_update to *true* so that time is not wasted in updating
# metdata every time an assignment happens.
Daru.lazy_update = true
# The 'new_with_size' function lets you specify the size of the
# vector as the argument and the block specifies how each element
# of the vector will be created.
a = Daru::Vector.new_with_size(samples) {r=rand(5); r==4 ? nil: r}
b = Daru::Vector.new_with_size(samples) {r=rand(5); r==4 ? nil: r}
# Call `#update` for updating the metadata of each vector, once
# creation from the block is complete.
a.update
b.update
# Pass the Daru::Vector objects in a Hash to the DataFrame constructor
# to make a DataFrame.
#
# The *order* option lets you specify the way the vectors in the Hash
# will be ordered. Not specifyin this will order vectors in alphabetical
# order by default.
ds = Daru::DataFrame.new({:a=>a,:b=>b}, order: [:b, :a])
# Reset lazy_update to *false* to prevent other code from breaking.
Daru.lazy_update = false
ds
Out[1]:
| Daru::DataFrame:19365600 rows: 1000 cols: 2 | ||
|---|---|---|
| b | a | |
| 0 | 2 | |
| 1 | 0 | 0 |
| 2 | 2 | |
| 3 | 2 | 3 |
| 4 | 3 | 2 |
| 5 | 2 | |
| 6 | 3 | 0 |
| 7 | 2 | 1 |
| 8 | 3 | |
| 9 | 2 | 1 |
| 10 | 3 | 3 |
| 11 | 2 | 3 |
| 12 | 1 | 3 |
| 13 | 2 | |
| 14 | 2 | 0 |
| 15 | 0 | |
| 16 | 1 | 3 |
| 17 | 1 | 2 |
| 18 | 0 | |
| 19 | 3 | 2 |
| 20 | 2 | 1 |
| 21 | 0 | 0 |
| 22 | 3 | |
| 23 | 0 | 1 |
| 24 | 0 | 1 |
| 25 | 3 | |
| 26 | ||
| 27 | 0 | |
| 28 | 0 | 1 |
| 29 | 3 | 1 |
| 30 | 2 | 3 |
| 31 | 2 | 1 |
| ... | ... | ... |
| 999 | 3 | 1 |