Package 'cncleanr'

Title: Parse Compact Numbers in Chinese Data
Description: Parses compact numeric values, qualified quantities, and ranges commonly found in Chinese tables and spreadsheets. It handles Chinese magnitude suffixes, currencies, percentages, full-width characters, financial negatives, and configurable missing-value markers while reporting values that cannot be parsed safely.
Authors: Haotian Liu [aut, cre, cph]
Maintainer: Haotian Liu <[email protected]>
License: MIT + file LICENSE
Version: 0.2.14
Built: 2026-10-16 01:39:09 UTC
Source: https://github.com/jorungandr/cncleanr

Help Index


Extract Problems From a cncleanr Result

Description

Returns parsing failures attached to an object produced by a cncleanr parser. Successful results return an empty data frame with the same stable schema. Use cn_subset() to select or reorder rows with their diagnostics. Other base R transformations may drop the attribute or retain stale indices.

Usage

cn_problems(x)

Arguments

x

An object returned by a cncleanr parsing function.

Value

A data frame with columns index, value, and reason.

Examples

result <- suppressWarnings(parse_cn_number(c("bad", "2\u4e07")))
cn_problems(result)

Select Parsed Results With Their Problems

Description

Explicitly subsets or reorders an unmodified parser result and remaps its problem indices to positions in the selected result. Repeated selections repeat the corresponding problems. Ordinary [ does not track problems.

Usage

cn_subset(x, rows)

Arguments

x

An unmodified result from a cncleanr parser, or from cn_subset().

rows

Integer-valued numeric indices (positive, zero, or negative using base R rules), or a logical vector of exactly the input length/row count. Missing, nonfinite, fractional and out-of-range indices are rejected. Defaults to all rows. Use integer() to select no rows.

Details

Call this before other transformations. It cannot recover problems lost or made stale by ordinary subsetting, mutation, sorting or binding. It does not reparse values or emit another parsing warning.

Value

The selected numeric vector or data frame, without a new class. Problem indices refer to the selected result, not the original input.

Examples

result <- suppressWarnings(parse_cn_number(c("bad", "2", "wrong")))
selected <- cn_subset(result, c(3, 2, 1, 3))
cn_problems(selected)
cn_subset(result, !is.na(result))

Parse Compact Numbers Used in Chinese Data

Description

Converts character values such as "1.25\u4e07", "\uffe53\u4ebf", "1.2e5", and "12.5%" to doubles. Parsing is deliberately conservative: every non-missing value must match the supported syntax in full. Spaces between digits are accepted only when they form valid three-digit grouping.

Usage

parse_cn_number(
  x,
  na = c(
    "", "NA", "N/A", "\u6682\u65e0", "\u672a\u516c\u5e03",
    "\u2014", "\u2013", "-", "...", "\u2026"
  ),
  strict = FALSE,
  unit = NULL
)

Arguments

x

A character, factor, or numeric vector. When unit = NULL, numeric input is converted to double and otherwise passed through unchanged, including Inf, -Inf, and NaN.

na

A character vector containing values that should be interpreted as missing. Matching happens after whitespace and full-width normalization.

strict

A single logical value. If FALSE, invalid values become NA_real_, a warning is emitted, and a data frame is stored in the problems attribute. If TRUE, invalid values cause an error.

unit

A single optional table-header unit: yuan, thousand yuan, ten thousand, hundred million, or trillion, optionally in yuan. Use the spellings "\u5143", "\u5343\u5143", "\u4e07", "\u4e07\u5143", "\u4ebf", "\u4ebf\u5143", "\u4e07\u4ebf", or "\u4e07\u4ebf\u5143". Bare values inherit this unit; explicit units must have the same multiplier and are not scaled twice. Percentages conflict with a header unit. With a header unit, numeric input follows character parsing: missing values remain missing, while infinities and overflow are reported as problems. Defaults to NULL (no header unit).

Details

Finite-range validation applies to parsed character and factor input. With unit = NULL, existing numeric Inf, -Inf, and NaN values are preserved. Results use double precision, not exact integer or decimal arithmetic. Nonzero character values rounded to zero after final unit conversion are reported as underflow; representable subnormal values are retained.

Value

A double vector with the same length and names as x. When invalid values occur in non-strict mode, the result has a problems attribute with columns index, value, and reason.

Examples

parse_cn_number(c("1.25\u4e07", "3\u4ebf\u5143", "12.5%", "\u6682\u65e0"))
parse_cn_number(c("\uff11\uff12\uff0e\uff15\uff05", "(2.5\u4e07)"))
parse_cn_number(c("123.5", "1.5\u5343\u5143"), unit = "\u5343\u5143")

Parse Qualified Quantities Used in Chinese Data

Description

Parses exact values and values qualified by language or symbols, such as "\u7ea63\u4e07", "\u5927\u4e8e2\u4ebf", "\u4e0d\u5c0f\u4e8e5\u4e07", "10\u4e07+", "50\u4f59", or "50\u4f59\u4e07\u5143". The qualifier is retained instead of silently discarded. One approximation prefix combined with an approximation suffix is accepted, for example "\u7ea615%\u5de6\u53f3". Repeated prefixes or suffixes and mixed inequality qualifiers are rejected.

Usage

parse_cn_quantity(
  x,
  na = c(
    "", "NA", "N/A", "\u6682\u65e0", "\u672a\u516c\u5e03",
    "\u2014", "\u2013", "-", "...", "\u2026"
  ),
  strict = FALSE
)

Arguments

x

A character, factor, or numeric vector. Numeric input is converted to double and otherwise passed through unchanged, including Inf, -Inf, and NaN.

na

A character vector containing values that should be interpreted as missing. Matching happens after whitespace and full-width normalization.

strict

A single logical value. If FALSE, invalid values become NA_real_, a warning is emitted, and a data frame is stored in the problems attribute. If TRUE, invalid values cause an error.

Value

A data frame with double column value and character column qualifier. Qualifiers are exact, approx, greater_than, at_least, less_than, and at_most. Numeric finite and infinite values have the qualifier exact; NA and NaN have a missing qualifier. Numeric values, including Inf, -Inf, and NaN, are preserved in value.

Examples

parse_cn_quantity(c(
  "\u7ea63\u4e07", "\u5927\u4e8e2\u4ebf", "10\u4e07+", "50\u4f59", "50\u4f59\u4e07\u5143"
))

Parse Numeric Ranges Used in Chinese Data

Description

Converts closed ranges such as "3\u4e07-5\u4e07" and open bounds such as "10\u4e07\u5143\u4ee5\u4e0a" into explicit lower and upper bounds. A shared suffix in "3-5\u4e07" is applied to both endpoints. Signs used by negative numbers and scientific notation are distinguished from range separators. Digit grouping is validated independently at each range endpoint, so "1 234-2 345" is accepted but "1 23-2 345" is not. Endpoint missing markers are checked before and after shared-unit propagation.

Usage

parse_cn_range(
  x,
  na = c(
    "", "NA", "N/A", "\u6682\u65e0", "\u672a\u516c\u5e03",
    "\u2014", "\u2013", "-", "...", "\u2026"
  ),
  strict = FALSE
)

Arguments

x

A character, factor, or numeric vector. Numeric input is converted to double and otherwise passed through unchanged, including Inf, -Inf, and NaN.

na

A character vector containing values that should be interpreted as missing. Matching happens after whitespace and full-width normalization.

strict

A single logical value. If FALSE, invalid values become NA_real_, a warning is emitted, and a data frame is stored in the problems attribute. If TRUE, invalid values cause an error.

Value

A data frame with columns lower, upper, lower_inclusive, and upper_inclusive. Numeric input becomes an inclusive point range, including numeric Inf and -Inf; NA and NaN have missing inclusivity. Infinite bounds created from textual inequalities represent open-ended bounds and are distinct from numeric point inputs.

Examples

parse_cn_range(c("3\u4e07-5\u4e07", "3-5\u4e07", "10\u4e07\u5143\u4ee5\u4e0a"))