Great answer Rob ! This is exactly what I was looking for !
Many Thanks !
Fred
Le lundi 28 septembre 2015 17:35:15 UTC+2, Rob J Goedman a écrit :
>
> Fred,
>
> Would below example work for you?
>
> *julia> **dft = readtable("/Users/rob/Desktop/test.csv", separator =
> '\t')*
> *8x5 DataFrames.DataFrame*
> *| Row | title1 | title2 | title3 | title4 | title5 |*
> *|-----|--------|--------|--------|--------|--------|*
> *| 1 | 10 | 20 | 30 | 40 | 50 |*
> *| 2 | 11 | 21 | 31 | 41 | 51 |*
> *| 3 | 12 | 22 | 32 | 42 | 52 |*
> *| 4 | 13 | 23 | 33 | 43 | 53 |*
> *| 5 | 14 | 24 | 34 | 44 | 54 |*
> *| 6 | 15 | 25 | 35 | 45 | 55 |*
> *| 7 | 16 | 26 | 36 | 46 | 56 |*
> *| 8 | 17 | 27 | 37 | 47 | 57 |*
>
> *julia> **titles = names(dft)*
> *5-element Array{Symbol,1}:*
> * :title1*
> * :title2*
> * :title3*
> * :title4*
> * :title5*
>
> *julia> **dft[[2:6], titles[2:5]]*
> *5x4 DataFrames.DataFrame*
> *| Row | title2 | title3 | title4 | title5 |*
> *|-----|--------|--------|--------|--------|*
> *| 1 | 21 | 31 | 41 | 51 |*
> *| 2 | 22 | 32 | 42 | 52 |*
> *| 3 | 23 | 33 | 43 | 53 |*
> *| 4 | 24 | 34 | 44 | 54 |*
> *| 5 | 25 | 35 | 45 | 55 |*
>
> *julia> **dft[[2:6], titles[3]]*
> *5-element DataArrays.DataArray{Int64,1}:*
> * 31*
> * 32*
> * 33*
> * 34*
> * 35*
>
>
> If you use list comprehension you will need the extra Symbol[] construct:
>
> *julia> **dft[:, Symbol[titles[i] for i in 2:3]]*
> *8x2 DataFrames.DataFrame*
> *| Row | title2 | title3 |*
> *|-----|--------|--------|*
> *| 1 | 20 | 30 |*
> *| 2 | 21 | 31 |*
> *| 3 | 22 | 32 |*
> *| 4 | 23 | 33 |*
> *| 5 | 24 | 34 |*
> *| 6 | 25 | 35 |*
> *| 7 | 26 | 36 |*
> *| 8 | 27 | 37 |*
>
>
> Regards,
> Rob
>
> On Sep 28, 2015, at 2:18 AM, Fred <[email protected] <javascript:>>
> wrote:
>
> Hi !
> I would like to know how is it possible to use variables in subsets of
> DataFrames ? I would like to use a syntax like
> df[:,:titles[1]] and df[:,:titles[1:3]]
>
> Thanks for your help !
>
>
> julia> using DataFrames
>
>
> julia> df = readtable("test.csv", separator = '\t')
> 8x5 DataFrame
> | Row | title1 | title2 | title3 | title4 | title5 |
> |-----|--------|--------|--------|--------|--------|
> | 1 | 10 | 20 | 30 | 40 | 50 |
> | 2 | 11 | 21 | 31 | 41 | 51 |
> | 3 | 12 | 22 | 32 | 42 | 52 |
> | 4 | 13 | 23 | 33 | 43 | 53 |
> | 5 | 14 | 24 | 34 | 44 | 54 |
> | 6 | 15 | 25 | 35 | 45 | 55 |
> | 7 | 16 | 26 | 36 | 46 | 56 |
> | 8 | 17 | 27 | 37 | 47 | 57 |
>
>
> julia> titles = readdlm("titles.csv")
> 3x1 Array{Any,2}:
> "title3"
> "title1"
> "title5"
>
>
> julia> df[:,:title2]
> 8-element DataArray{Int64,1}:
>
> 20
>
> 21
>
> 22
>
> 23
>
> 24
>
> 25
>
> 26
>
> 27
>
>
>
> julia> titles[1]
> "title3"
>
>
>
> julia> df[:,:titles[1]]
> ERROR: `getindex` has no method matching getindex(::Symbol, ::Int64)
>
> julia> df[:,:titles[1:3]]
> ERROR: `getindex` has no method matching getindex(::Symbol, ::UnitRange{
> Int64})
>
>
>
> <test.csv><titles.csv>
>
>
>