Skip to content

@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
5×5 DataFrame
RowTitleYearLengthBudgetRating
StringInt32Int32Int32?Float64
1$1971121missing6.4
2$1000 a Touchdown193971missing6.0
3$21 a Day Once a Month19417missing8.2
4$40,000199670missing8.2
5$50,000 Climax Show, The197571missing3.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
4×5 DataFrame
RowTitleYearLengthBudgetRating
StringInt32Int32Int32?Float64
1$1971121missing6.4
2$21 a Day Once a Month19417missing8.2
3$50,000 Climax Show, The197571missing3.4
4$windle200293missing5.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
4×5 DataFrame
RowTitleYearLengthBudgetRating
StringInt32Int32Int32?Float64
1$1971121missing6.4
2$21 a Day Once a Month19417missing8.2
3$50,000 Climax Show, The197571missing3.4
4$windle200293missing5.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
6×5 DataFrame
RowTitleYearLengthBudgetRating
StringInt32Int32Int32?Float64
1$1971121missing6.4
2$1000 a Touchdown193971missing6.0
3$21 a Day Once a Month19417missing8.2
4$40,000199670missing8.2
5$50,000 Climax Show, The197571missing3.4
6'49-'17191761missing6.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
5×5 DataFrame
RowTitleYearLengthBudgetRating
StringInt32Int32Int32?Float64
1$pent200091missing4.3
2$windle200293missing5.3
3'15'200225missing6.7
4'38198797missing6.6
5'49-'17191761missing6.0

This page was generated using Literate.jl.