6 Creating and modifying columns
These operations add new variables, preserving existing ones. This operation uses both vectorized and regular operations.
syntax
(create df [new-column (binder ...) body ...] ...)
binder = bound-column | [bound-column : type] type = element | vector
df : (or/c data-frame? grouped-data-frame?)
Each new column is specified by a single clause. The column created will have the name new-column, and be specified by the expressions in body.
The bound variables in body are specified by binder. Each bound variable either has the type element, which binds a single element of the given column and maps over it, or vector, which binds the entire column. If a type for a bound variable is not specified, it defaults to element.
The binding structure of create is like let*: columns can depend on those coming in previous clauses, but not the other way around.
If every bound variable in a given column specification is of type vector, it is expected that body produces a vector of the same length as all other columns. Otherwise, it is expected that body produces some quantity, and it will be mapped over every column specified by variables of type element.
> (define (v/ vec c) (vector-map (Ξ» (v) (/ v c)) vec))
> (define (sum vec) (for/sum ([v (in-vector vec)]) v))
> (~> example-df (create [total (adult juv) (+ adult juv)] [grp (grp) (string-append "blerg" grp)] [freq ([juv : vector]) (v/ juv (sum juv))]) show)
data-frame: 5 rows x 6 columns
ββββββββ¬βββββ¬ββββ¬ββββββ¬ββββ¬ββββββ
βgrp βfreqβtrtβadultβjuvβtotalβ
ββββββββΌβββββΌββββΌββββββΌββββΌββββββ€
βblergaβ1/15βb β1 β10 β11 β
ββββββββΌβββββΌββββΌββββββΌββββΌββββββ€
βblergaβ2/15βb β2 β20 β22 β
ββββββββΌβββββΌββββΌββββββΌββββΌββββββ€
βblergbβ1/5 βa β3 β30 β33 β
ββββββββΌβββββΌββββΌββββββΌββββΌββββββ€
βblergbβ4/15βb β4 β40 β44 β
ββββββββΌβββββΌββββΌββββββΌββββΌββββββ€
βblergbβ1/3 βb β5 β50 β55 β
ββββββββ΄βββββ΄ββββ΄ββββββ΄ββββ΄ββββββ
procedure
(rename df from to ...) β (or/c data-frame? grouped-data-frame?)
df : (or/c data-frame? grouped-data-frame?) from : string? to : string?
> (~> example-df (rename "grp" "waldo" "trt" "warbly") show)
data-frame: 5 rows x 4 columns
βββββββ¬ββββββ¬ββββ¬βββββββ
βadultβwaldoβjuvβwarblyβ
βββββββΌββββββΌββββΌβββββββ€
β1 βa β10 βb β
βββββββΌββββββΌββββΌβββββββ€
β2 βa β20 βb β
βββββββΌββββββΌββββΌβββββββ€
β3 βb β30 βa β
βββββββΌββββββΌββββΌβββββββ€
β4 βb β40 βb β
βββββββΌββββββΌββββΌβββββββ€
β5 βb β50 βb β
βββββββ΄ββββββ΄ββββ΄βββββββ