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

Replace sortperm by alternative pattern #338

Merged
merged 6 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DataFramesMeta = "1313f7d8-7da2-5740-9ea0-a2ca25f37964"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
Documenter = "0.27"
20 changes: 17 additions & 3 deletions docs/src/dplyr.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,33 @@ Same as above, except here we filter the rows for mammals that sleep for 16 or m
end
```

Something slightly more complicated: same as above, except arrange the rows in the `:sleep_total` column in a descending order. For this, use the function `sortperm` with the keyword argument `rev=true`.
Something slightly more complicated: same as above, except arrange the rows in the `:sleep_total` column in a descending order. Since this column is numeric we can do it by negating it:

```@repl 1
@chain msleep begin
@select :name :order :sleep_total
@orderby begin
:order
sortperm(:sleep_total, rev=true)
:order
-:sleep_total
end
@rsubset :sleep_total >= 16
end
```

Alternatively, using the StatsBase.jl package you could have written `ordinalrank(:sleep_total, rev=true)`.
Finally, if you wanted to avoid using an additional package you could use the following expression `invperm(sortperm(:sleep_total, rev=true))`.

Here is a minimal example of sorting string column in reverse:

```@repl 1
using StatsBase
df = DataFrame(group=[1, 2, 1, 2, 1], name = ["Bob", "Dexter", "Alice", "Eve", "Cedric"])
@orderby df begin
:group
ordinalrank(:name, rev=true)
end
```

## Create New Columns Using `@transform` and `@rtransform`

The `@transform` macro will add new columns to the data frame. Like with other macros, use `@rtransform` to operate row-wise. Create a new column called `:rem_proportion`, which is the ratio of rem sleep to total amount of sleep.
Expand Down
42 changes: 21 additions & 21 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1214,38 +1214,38 @@ $ASTABLE_RHS_ORDERBY_DOCS
julia> using DataFramesMeta, Statistics

julia> d = DataFrame(x = [3, 3, 3, 2, 1, 1, 1, 2, 1, 1], n = 1:10,
c = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]);
c = ["a", "c", "b", "e", "d", "g", "f", "i", "j", "h"]);

julia> @orderby(d, -1 .* :n)
julia> @orderby(d, -:n)
10×3 DataFrame
Row │ x n c
│ Int64 Int64 String
─────┼──────────────────────
1 │ 1 10 j
2 │ 1 9 i
3 │ 2 8 h
4 │ 1 7 g
5 │ 1 6 f
6 │ 1 5 e
7 │ 2 4 d
8 │ 3 3 c
9 │ 3 2 b
1 │ 1 10 h
2 │ 1 9 j
3 │ 2 8 i
4 │ 1 7 f
5 │ 1 6 g
6 │ 1 5 d
7 │ 2 4 e
8 │ 3 3 b
9 │ 3 2 c
10 │ 3 1 a

julia> @orderby(d, sortperm(:c, rev = true))
julia> @orderby(d, invperm(sortperm(:c, rev = true)))
10×3 DataFrame
Row │ x n c
│ Int64 Int64 String
─────┼──────────────────────
1 │ 1 10 j
2 │ 1 9 i
3 │ 2 8 h
4 │ 1 7 g
5 │ 1 6 f
6 │ 1 5 e
7 │ 2 4 d
8 │ 3 3 c
9 │ 3 2 b
1 │ 1 9 j
2 │ 2 8 i
3 │ 1 10 h
4 │ 1 6 g
5 │ 1 7 f
6 │ 2 4 e
7 │ 1 5 d
8 │ 3 2 c
9 │ 3 3 b
10 │ 3 1 a

julia> @orderby d begin
Expand Down