as-numeric

Coerce a column to numeric, or tag an aesthetic as continuous.

Two call forms: when given (data, col) it returns a new dataset with col parsed as a number in every row; when given (col) alone it returns a mapping-ref annotation that aes accepts in place of a column name, forcing the scale system to treat that channel as continuous.

Usage

as-numeric(data, col)
as-numeric(col)

Arities

  • as-numeric(data, col): Return a new dataset with col converted to numbers via parse-number.
  • as-numeric(col): Return a mapping-ref dict tagging col as continuous for aes.

Parameters

Parameter Default Description
..args Either (data, col) or (col). See arities.

Returns

New dataset (2-arg) or mapping-ref dict (1-arg).

Examples

Two-arg form rewrites a column with parsed numbers; useful when the data arrived as strings.

#let raw = (
  (x: "1", y: 2.0),
  (x: "2", y: 4.0),
  (x: "3", y: 9.0),
)
#let d = as-numeric(raw, "x")
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (geom-point(size: 3pt),),
  width: 10cm,
  height: 6cm,
)

Scatter chart of y against x after the string x column has been parsed to numbers, plotted on a continuous x axis.

Scatter chart of y against x after the string x column has been parsed to numbers, plotted on a continuous x axis.

One-arg form tags the column inside aes so the scale picks continuous semantics without rewriting the dataset.

#let raw = (
  (x: 1, y: 2.0),
  (x: 2, y: 4.0),
  (x: 3, y: 9.0),
)
#plot(
  data: raw,
  mapping: aes(x: as-numeric("x"), y: "y"),
  layers: (geom-point(size: 3pt),),
  width: 10cm,
  height: 6cm,
)

Scatter chart of y against x where the x channel is tagged as continuous inline via as-numeric, leaving the dataset unchanged.

Scatter chart of y against x where the x channel is tagged as continuous inline via as-numeric, leaving the dataset unchanged.

See also

as-factor, aes.

Back to top