@slice
Slicing rows is similar to filtering rows, except that slicing is performed based on row numbers rather tha filter criteria. In Tidier.jl
, slicing works similarly to R's tidyverse
in that both positive (which rows to keep) and negative (which rows to remove) slicing is supported. For @slice()
, any valid UnitRange
of integers is considered valid; this is not the case for @select()
or across()
.
Remember: Just like every other Tidier.jl
top-level macro, @slice()
respects group. This means that in a grouped data frame, @slice(1:2)
will select the first 2 rows from each group.
using Tidier
using RDatasets
movies = dataset("ggplot2", "movies");
Slicing using a range of numbers¤
This is an easy way of retrieving 5 consecutive rows.
@chain movies begin
@slice(1:5)
@select(1:5)
end
Row | Title | Year | Length | Budget | Rating |
---|---|---|---|---|---|
String | Int32 | Int32 | Int32? | Float64 | |
1 | $ | 1971 | 121 | missing | 6.4 |
2 | $1000 a Touchdown | 1939 | 71 | missing | 6.0 |
3 | $21 a Day Once a Month | 1941 | 7 | missing | 8.2 |
4 | $40,000 | 1996 | 70 | missing | 8.2 |
5 | $50,000 Climax Show, The | 1975 | 71 | missing | 3.4 |
Slicing using a more complex UnitRange of numbers¤
How would we obtain every other from 1 to 7 (counting up by 2)? Note that range()
is similar to seq()
in R.
@chain movies begin
@slice(range(start = 1, step = 2, stop = 7))
@select(1:5)
end
Row | Title | Year | Length | Budget | Rating |
---|---|---|---|---|---|
String | Int32 | Int32 | Int32? | Float64 | |
1 | $ | 1971 | 121 | missing | 6.4 |
2 | $21 a Day Once a Month | 1941 | 7 | missing | 8.2 |
3 | $50,000 Climax Show, The | 1975 | 71 | missing | 3.4 |
4 | $windle | 2002 | 93 | missing | 5.3 |
This same code can also be written using Julia's shorthand syntax for unit ranges.
@chain movies begin
@slice(1:2:7)
@select(1:5)
end
Row | Title | Year | Length | Budget | Rating |
---|---|---|---|---|---|
String | Int32 | Int32 | Int32? | Float64 | |
1 | $ | 1971 | 121 | missing | 6.4 |
2 | $21 a Day Once a Month | 1941 | 7 | missing | 8.2 |
3 | $50,000 Climax Show, The | 1975 | 71 | missing | 3.4 |
4 | $windle | 2002 | 93 | missing | 5.3 |
Separate multiple row selections with commas¤
If you have multiple different row selections, you can separate them with commas.
@chain movies begin
@slice(1:5, 10)
@select(1:5)
end
Row | Title | Year | Length | Budget | Rating |
---|---|---|---|---|---|
String | Int32 | Int32 | Int32? | Float64 | |
1 | $ | 1971 | 121 | missing | 6.4 |
2 | $1000 a Touchdown | 1939 | 71 | missing | 6.0 |
3 | $21 a Day Once a Month | 1941 | 7 | missing | 8.2 |
4 | $40,000 | 1996 | 70 | missing | 8.2 |
5 | $50,000 Climax Show, The | 1975 | 71 | missing | 3.4 |
6 | '49-'17 | 1917 | 61 | missing | 6.0 |
Inverted selection using negative numbers¤
This line selects all rows except the first 5 rows. For the sake of brevity, we are only showing the first 5 of the remaining results.
@chain movies begin
@slice(-(1:5))
@select(1:5)
@slice(1:5)
end
Row | Title | Year | Length | Budget | Rating |
---|---|---|---|---|---|
String | Int32 | Int32 | Int32? | Float64 | |
1 | $pent | 2000 | 91 | missing | 4.3 |
2 | $windle | 2002 | 93 | missing | 5.3 |
3 | '15' | 2002 | 25 | missing | 6.7 |
4 | '38 | 1987 | 97 | missing | 6.6 |
5 | '49-'17 | 1917 | 61 | missing | 6.0 |
This page was generated using Literate.jl.