Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rebased version for #2327 add filter subsetting to docs #2598

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions docs/src/man/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,58 @@ package provides interfaces similar to LINQ and [dplyr](https://dplyr.tidyverse.

See the [Data manipulation frameworks](@ref) section for more information.

#### Selecting rows with `filter` and `subset`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put this section above the one presenting select and transform, which is more complex and goes beyond taking subsets. (Transformations probably deserve a separate section so that we only present columns selection, but that's another story.)

That way you don't need to repeat the example: you can just continue with the ones given at the end of the Indexing section.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot, as subset has the same syntax as select so we need to introduce it after filter. We could split the discussion of filter and subset though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah indeed. Let's keep it that way then. But "Taking a Subset" isn't actually a good name for the parent section as it also shows complex transformations. Maybe we should just split each sub-section into its own section, i.e. "Indexing", "Selecting and transforming columns" and "Selecting rows".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.


We have seen above how to subset a `DataFrame` to several criteria, involving multiple columns,
by supplying a logical vector to the first dimension.
For instance, in the following we want to subset to all rows where `x > 2` and where `a == 'c'`:

```jldoctest dataframe
julia> df = DataFrame(x=1:4, y=["Alice", "Bob", "Claire", "Dylan"], a='a':'d')
4×3 DataFrame
Row │ x y a
│ Int64 String Char
─────┼─────────────────────
1 │ 1 Alice a
2 │ 2 Bob b
3 │ 3 Claire c
4 │ 4 Dylan d

julia> df[(df.x .> 2) .& (df.a .== 'c'), : ]
1×3 DataFrame
Row │ x y a
│ Int64 String Char
─────┼─────────────────────
1 │ 3 Claire c
```

An alternative formulation, which notably saves on the need to use
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we provide subset with a syntax closer to select, I'm not sure we should mention filter at all in Getting Started. Or maybe just mention it at the end for reference. What do you think @bkamins?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do this - then it should be after select section as commented above. So - should I make this change?

broadcasting syntax via `.` prefixes, uses [`filter`](@ref) or [`filter!`](@ref):

```jldoctest dataframe
julia> filter([:x, :a] => (x, a) -> x > 2 && a == 'c', df)
1×3 DataFrame
Row │ x y a
│ Int64 String Char
─────┼─────────────────────
1 │ 3 Claire c
```

You can also use the [`subset`](@ref) or [`subset!`](@ref) functions that are more
flexible. They use the same syntax as [`select`](@ref) that was discussed in
the previous section). The requirement is that each column transformation
must produce a vector of `Bool` and only the rows from the source data frame
for which all transformations produce `true` are included in the result:

```jldoctest dataframe
julia> subset(df, :x => ByRow(>(2)), :a => ByRow(==('c')))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe start showing the standard x -> x > 2 syntax? That's easier to grasp for newcomers, and more general.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - I will update it when we settle on the general layout.

1×3 DataFrame
Row │ x y a
│ Int64 String Char
─────┼─────────────────────
1 │ 3 Claire c
```

### Summarizing Data

The `describe` function returns a data frame summarizing the elementary statistics and information about each column:
Expand Down