5 Slicing
This operation subsets a data frame, returning columns specified by a smaller expression language.
syntax
(slice df slice-spec)
slice-spec = string | [string-literal ...] | regexp | everything | (or slice-spec ...) | (and slice-spec ...) | (not slice-spec) | (all-in string-sequence) | (any-in string-sequence) | (starting-with string) | (ending-with string) | (containing string)
df : (or/c data-frame? grouped-data-frame?)
string : string?
regexp : regexp?
string-literal : string?
string-sequence : (sequence/c string?)
[str ...]
str : string? Selects the columns with names str. This doesnβt have to be brackets, but it is recommended for readabilityβs sake.
The strings supplied here must be string literals, or else syntax errors would be too poor. If you want to use a variable, use all-in or any-in.
syntax
Selects every column in the given data-frame.
syntax
(or spec ...)
Selects the union of the given specs.
syntax
(and spec ...)
Selects the intersection of the given specs.
syntax
(not spec ...)
Selects the complement of (everything but) the given specs.
syntax
(all-in sequence)
sequence : (sequence/c string?) Selects all variables with names in the given sequence. If a name is present in sequence but not the data-frame, this errors.The input sequence cannot be infinite, or this does not terminate.
syntax
(any-in sequence)
sequence : (sequence/c string?) Like all-in, but does not error when a name is not present in sequence, and merely does not select it.
syntax
(starting-with suffix)
prefix : string? Selects columns with names beginning with the given prefix.
syntax
(ending-with suffix)
suffix : string? Selects columns with names ending with the given suffix.
syntax
(containing substr)
substr : string? Selects columns with names containing the given substr.
Using these outside of the context of slice is a syntax error (aside from and, or, and not, for obvious reasons).
This operation will not remove variables that a grouped data frame is grouped by, as this would destroy group invariants.
> (~> example-df (slice "trt") show)
data-frame: 5 rows x 1 columns
βββββ
βtrtβ
βββββ€
βb β
βββββ€
βb β
βββββ€
βa β
βββββ€
βb β
βββββ€
βb β
βββββ
> (~> example-df (slice (not ["trt" "grp"])) show)
data-frame: 5 rows x 2 columns
βββββ¬ββββββ
βjuvβadultβ
βββββΌββββββ€
β10 β1 β
βββββΌββββββ€
β20 β2 β
βββββΌββββββ€
β30 β3 β
βββββΌββββββ€
β40 β4 β
βββββΌββββββ€
β50 β5 β
βββββ΄ββββββ
> (~> example-df (slice (containing "t")) show)
data-frame: 5 rows x 2 columns
βββββ¬ββββββ
βtrtβadultβ
βββββΌββββββ€
βb β1 β
βββββΌββββββ€
βb β2 β
βββββΌββββββ€
βa β3 β
βββββΌββββββ€
βb β4 β
βββββΌββββββ€
βb β5 β
βββββ΄ββββββ
procedure
(take-rows df beg end) β (or/c data-frame? grouped-data-frame?)
df : (or/c data-frame? grouped-data-frame?) beg : exact-nonnegative-integer? end : exact-nonnegative-integer?
If df is grouped, this takes rows from inside each group.
> (~> example-df (take-rows 0 3) show)
data-frame: 3 rows x 4 columns
βββββ¬ββββ¬ββββββ¬ββββ
βgrpβtrtβadultβjuvβ
βββββΌββββΌββββββΌββββ€
βa βb β1 β10 β
βββββΌββββΌββββββΌββββ€
βa βb β2 β20 β
βββββΌββββΌββββββΌββββ€
βb βa β3 β30 β
βββββ΄ββββ΄ββββββ΄ββββ
> (~> example-df (group-with "trt") (take-rows 0 1) show)
data-frame: 2 rows x 4 columns
groups: (trt)
βββββ¬ββββ¬ββββ¬ββββββ
βjuvβtrtβgrpβadultβ
βββββΌββββΌββββΌββββββ€
β30 βa βb β3 β
βββββΌββββΌββββΌββββββ€
β10 βb βa β1 β
βββββ΄ββββ΄ββββ΄ββββββ