Archive for category julia

Convert DataArray taken from a DataFrame to an Array / Vector in Julia


julia> DataFrame(CCnt=1:10,Alpha=21:30)
10x2 DataFrame:
CCnt Alpha
[1,] 1 21
[2,] 2 22
[3,] 3 23
[4,] 4 24
[5,] 5 25
[6,] 6 26
[7,] 7 27
[8,] 8 28
[9,] 9 29
[10,] 10 30

julia> samples = DataFrame(CCnt=1:10,Alpha=21:30)
10x2 DataFrame:
CCnt Alpha
[1,] 1 21
[2,] 2 22
[3,] 3 23
[4,] 4 24
[5,] 5 25
[6,] 6 26
[7,] 7 27
[8,] 8 28
[9,] 9 29
[10,] 10 30

julia> samples[:CCnt]
10-element DataArray{Int64,1}:
1
2
3
4
5
6
7
8
9
10

julia> vector(samples[:CCnt])
10-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
10

Tags:

Add / Concat / append / rbind row to Julia DataFrame

In Julia you use vcat to add or append or concatenate a row of data to a Julia DataFrame.

Example:

julia> mydf = DataFrame(X=[0:10],Y=[100:110])
11x2 DataFrame:
X Y
[1,] 0 100
[2,] 1 101
[3,] 2 102
[4,] 3 103
[5,] 4 104
[6,] 5 105
[7,] 6 106
[8,] 7 107
[9,] 8 108
[10,] 9 109
[11,] 10 110

julia> mydf = vcat(mydf,DataFrame(X=12,Y=15))
12x2 DataFrame:
X Y
[1,] 0 100
[2,] 1 101
[3,] 2 102
[4,] 3 103
[5,] 4 104
[6,] 5 105
[7,] 6 106
[8,] 7 107
[9,] 8 108
[10,] 9 109
[11,] 10 110
[12,] 12 15

Tags:

Index a DataFrame subset on string column name in Julia


julia> using RDatasets

julia> iris = dataset("datasets", "iris")

julia> iris[iris[:Species] .== "setosa", :]
50x5 DataFrame
|-------|-------------|------------|-------------|------------|----------|
| Row # | SepalLength | SepalWidth | PetalLength | PetalWidth | Species |
| 1 | 5.1 | 3.5 | 1.4 | 0.2 | "setosa" |
| 2 | 4.9 | 3.0 | 1.4 | 0.2 | "setosa" |
| 3 | 4.7 | 3.2 | 1.3 | 0.2 | "setosa" |
| 4 | 4.6 | 3.1 | 1.5 | 0.2 | "setosa" |
| 5 | 5.0 | 3.6 | 1.4 | 0.2 | "setosa" |
| 6 | 5.4 | 3.9 | 1.7 | 0.4 | "setosa" |
| 7 | 4.6 | 3.4 | 1.4 | 0.3 | "setosa" |
| 8 | 5.0 | 3.4 | 1.5 | 0.2 | "setosa" |
| 9 | 4.4 | 2.9 | 1.4 | 0.2 | "setosa" |

Tags: , ,

Nice post on Julia meta operations

Great post about basic Julia stuff @ Julia Helps

Tags:

Sample with replacement from DataFrame in Julia

julia> df = DataFrame(A = 1:10, B = 2:2:20)

10x2 DataFrame
|-------|----|----|
| Row # | A | B |
| 1 | 1 | 2 |
| 2 | 2 | 4 |
| 3 | 3 | 6 |
| 4 | 4 | 8 |
| 5 | 5 | 10 |
| 6 | 6 | 12 |
| 7 | 7 | 14 |
| 8 | 8 | 16 |
| 9 | 9 | 18 |
| 10 | 10 | 20 |

julia> df[sample([1:size(df,1)],20),:]

20x2 DataFrame
|-------|----|----|
| Row # | A | B |
| 1 | 2 | 4 |
| 2 | 3 | 6 |
| 3 | 1 | 2 |
| 4 | 4 | 8 |
| 5 | 3 | 6 |
| 6 | 5 | 10 |
| 7 | 8 | 16 |
| 8 | 5 | 10 |
| 9 | 4 | 8 |
| 10 | 8 | 16 |
| 11 | 9 | 18 |
| 12 | 10 | 20 |
| 13 | 9 | 18 |
| 14 | 4 | 8 |
| 15 | 1 | 2 |
| 16 | 1 | 2 |
| 17 | 3 | 6 |
| 18 | 4 | 8 |
| 19 | 10 | 20 |
| 20 | 7 | 14 |

Tags:

Convert a matrix (Array) of Any to a matrix (Array) of floats in Julia


convert(Array{Float64},array_of_Anys)

Tags:

Sample with replacement from Matrix in Julia

julia> sample([1 2 3;4 5 6;7 8 9], 10)
10-element Array{Int64,1}:
1
3
7
3
8
6
3
7
5
4

Tags:

Sample with replacement from a vector in Julia

julia> sample([1 2 3], 10)
10-element Array{Int64,1}:
2
3
2
3
1
1
2
1
1
1

Tags:

Index of elements in array that fulfils a condition in Julia

In Julia (julialang.org) if you are rather interested in finding the indices (indexes) of the elements in an array that fulfils a certain boolean condition then find is your friend, like so:


julia> probs = [0.1, 0.2, 0.3, 0.4, 0.5]
5-element Array{Float64,1}:
0.1
0.2
0.3
0.4
0.5

julia> find(x->x>0.2, probs)
3-element Array{Int64,1}:
3
4
5

Conditional Array indexing in Julia

Conditional array indexing is a really nice convenience when one wants to access array items that fullfills a certain boolean criteria. This is done in Julia in the following way:


julia> probs = [0.1, 0.2, 0.3, 0.4, 0.5]
5-element Array{Float64,1}:
0.1
0.2
0.3
0.4
0.5

julia> probs[probs.>0.2]
3-element Array{Float64,1}:
0.3
0.4
0.5

Observe the “.” in the .> operator which signifies elemetwise comparison.
If you omit the “.” you will get the following error:


probs[probs>0.2]
ERROR: no method isless(Float64,Array{Float64,1})
in > at operators.jl:19

Tags: