Skip to content

Commit 54e02ce

Browse files
greimelnalimilan
andcommitted
Apply suggestions from code review
Co-Authored-By: Milan Bouchet-Valat <[email protected]>
1 parent e90a02f commit 54e02ce

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

src/extras.jl

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ also accept them.
6565
outside of the breaks; when `true`, breaks are automatically added to include all
6666
values in `x`, and the upper bound is included in the last interval.
6767
* `labels::Union{AbstractVector,Function}: a vector of strings giving the names to use for
68-
the intervals; or a function `f(from, to, i; extend)` that generates the labels from the
68+
the intervals; or a function `f(from, to, i; closed)` that generates the labels from the
6969
left and right interval boundaries and the group index. Defaults to
70-
`string("[", from, ", ", to, extend ? "]" : ")")`, e.g. `"[1, 5)"`.
70+
`"[from, to)"` (or `"[from, to]"` for the rightmost interval if `extend == true`).
7171
* `allow_missing::Bool=true`: when `true`, values outside of breaks result in missing values.
7272
only supported when `x` accepts missing values.
7373
7474
# Examples
7575
```jldoctest
7676
julia> using CategoricalArrays
7777
78-
julia> x = collect(-1:0.5:1);
78+
julia> x = -1:0.5:1;
7979
8080
julia> cut(x, [0, 1], extend=true)
8181
5-element CategoricalArray{String,1,UInt32}:
@@ -85,6 +85,8 @@ julia> cut(x, [0, 1], extend=true)
8585
"[0.0, 1.0]"
8686
"[0.0, 1.0]"
8787
88+
julia> x = -1:0.5:1;
89+
8890
julia> cut(x, 2)
8991
5-element CategoricalArray{String,1,UInt32}:
9092
"[-1.0, 0.0)"
@@ -93,6 +95,8 @@ julia> cut(x, 2)
9395
"[0.0, 1.0]"
9496
"[0.0, 1.0]"
9597
98+
julia> x = -1:0.5:1;
99+
96100
julia> cut(x, 2, labels=["A", "B"])
97101
5-element CategoricalArray{String,1,UInt32}:
98102
"A"
@@ -111,22 +115,6 @@ julia> cut(x, 3, labels=fmt)
111115
"grp 2 (-0.333333//0.333333)"
112116
"grp 3 (0.333333//1.0)"
113117
"grp 3 (0.333333//1.0)"
114-
115-
julia> using StatsBase: ecdf
116-
117-
julia> percentile(x) = round(Int,100*parse(Float64,x))
118-
percentile (generic function with 1 method)
119-
120-
julia> fmt2(from, to, i; closed) = "P$(percentile(from))P$(percentile(to))"
121-
fmt2 (generic function with 1 method)
122-
123-
julia> cut(ecdf(x)(x), 3, labels=fmt2)
124-
5-element CategoricalArray{String,1,UInt32}:
125-
"P20P47"
126-
"P20P47"
127-
"P47P73"
128-
"P73P100"
129-
"P73P100"
130118
```
131119
"""
132120
function cut(x::AbstractArray{T, N}, breaks::AbstractVector;

test/15_extras.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ end
111111
@testset "formatter function" begin
112112
my_formatter(from, to, i; extend) = "$i: $from -- $to"
113113

114-
x = collect(0.15:0.20:0.95)
114+
x = 0.15:0.20:0.95
115115
p = [0, 0.4, 0.8, 1.0]
116116

117-
@test cut(x, p, labels=my_formatter2) == ["1: 0.0 -- 0.4", "1: 0.0 -- 0.4", "2: 0.4 -- 0.8", "2: 0.4 -- 0.8", "3: 0.8 -- 1.0"]
117+
@test cut(x, p, labels=my_formatter2) ==
118+
["1: 0.0 -- 0.4", "1: 0.0 -- 0.4", "2: 0.4 -- 0.8", "2: 0.4 -- 0.8", "3: 0.8 -- 1.0"]
118119
end
119120

120121
end

0 commit comments

Comments
 (0)