Here are a couple of ways:

julia> df = DataFrame(x1 = 1:3, x2 = [2, 1, 2],x3 = [22, 21, 20]);

julia> df[(df[:x3] .< 22) & (df[:x2] .== 2), :]
1x3 DataFrames.DataFrame
| Row | x1 | x2 | x3 |
|-----|----|----|----|
| 1   | 3  | 2  | 20 |

julia> using DataFramesMeta

julia> @where(df, (:x3 .< 22) & (:x2 .== 2))
1x3 DataFrames.DataFrame
| Row | x1 | x2 | x3 |
|-----|----|----|----|
| 1   | 3  | 2  | 20 |


On Mon, Mar 2, 2015 at 3:09 AM, bernhard <[email protected]> wrote:

> Hi all
>
> Can anyone tell me how to make this work in the current version of Julia?
> v"0.3.4"
> I would even prefer to be able to evaluate an expression and receive the
> index of the rows which match the results. The code could look similar to
> this
>
> df = DataFrame(x1 = 1:3, x2 = [2, 1, 2],x3 = [22, 21, 20])
> ex=:((x3 .< 22) & (x2 .== 2))
>
> rows_matching_the_condition=df[eval(ex)] #if this would work, it would
> return the subset I guess and not the index vector though
>
>
> Am Freitag, 27. September 2013 22:38:49 UTC+2 schrieb Stefan Karpinski:
>
>> Using single & and | should work too.
>>
>> On Friday, September 27, 2013, Andrew Gendreau wrote:
>>
>>> Makes sense, thanks Joosep
>>>
>>> On Friday, September 27, 2013 4:04:13 PM UTC-4, Joosep Pata wrote:
>>>>
>>>> The following works as one would expect.
>>>>
>>>> df[:((a .== 1) .+ (b .== 2)), :] #or
>>>> df[:((a .== 1) .* (b .== 2)), :] #and
>>>>
>>>> My non-expert explanation is the following:
>>>> both subexpressions return a DataArray of bools, for which the logical
>>>> operator && is not defined (what would it mean for an array to have a
>>>> single true/false value?), but the elementwise product via .* is. The
>>>> DataFrame is "masked" via the resulting vector of bools.
>>>>
>>>> Joosep
>>>>
>>>> On Sep 27, 2013, at 10:49 PM, Andrew Gendreau <[email protected]>
>>>> wrote:
>>>>
>>>> > Hi,
>>>> >
>>>> > I'm just getting started with working with programming languages in
>>>> general and am trying to learn Julia alongside R.  I'm running into a
>>>> problem when subsetting within DataFrames in Julia, specifically when I try
>>>> to do so based on two conditions.  For reference, I'm working off the
>>>> syntax on the DataFrames subsetting page:  http://juliastats.github.io/
>>>> DataFrames.jl/subsets.html
>>>> >
>>>> > These work as expected:
>>>> >
>>>> > julia> df2[:(Ozone .> 5), :]
>>>> > 5x2 DataFrame:
>>>> >         Ozone Temp
>>>> > [1,]        6   86
>>>> > [2,]        7   87
>>>> > [3,]        8   88
>>>> > [4,]        9   89
>>>> > [5,]       10   90
>>>> >
>>>> >
>>>> > julia> df2[:(Temp .> 88), :]
>>>> > 2x2 DataFrame:
>>>> >         Ozone Temp
>>>> > [1,]        9   89
>>>> > [2,]       10   90
>>>> >
>>>> >
>>>> > But when I try to combine them:
>>>> >
>>>> >
>>>> > julia> df2[:(Ozone .> 5 && Temp .> 88), :]
>>>> > ERROR: type: non-boolean (DataArray{Bool,1}) used in boolean context
>>>> >  in anonymous at C:\Users\andrew.gendreau\AppData\Roaming\Julia\
>>>> packages\DataFrames\src\dataframe.jl:1466
>>>> >
>>>> >
>>>> > julia> df2[:(Ozone .> 5 & Temp .> 88), :]
>>>> > 0x2 DataFrame:
>>>> >   Ozone Temp
>>>> >
>>>> >
>>>> > Any ideas on what I'm doing wrong?   select(:(Ozone .> 5 & Temp .>
>>>> 88), df2)  doesn't seem to work either (returns an empty SubDataFrame).
>>>> >
>>>> > Thank you,
>>>> > Andrew
>>>>
>>>>

Reply via email to