Learn R Programming

Tivy (version 0.1.1)

extract_numeric_values: Extract length values from column names

Description

Helper function to extract numerical length values from column names. Handles different naming patterns like "length_8.5", "weighted_9", "8", etc. Uses multiple extraction strategies to ensure robust parsing of column names.

Usage

extract_numeric_values(
  column_names,
  use_fallback = TRUE,
  fallback_type = "sequential",
  verbose = FALSE
)

Value

Numeric vector of length values extracted from column names.

Arguments

column_names

Character vector of column names.

use_fallback

Logical. If TRUE, uses fallback strategy when numeric values cannot be extracted.

fallback_type

Character. Type of fallback to use when use_fallback = TRUE. Options: "sequential", "ones", "zeros".

verbose

Logical. Print information about extraction strategy used.

Details

The function uses the following extraction strategies in order:

  1. Specific prefixes: "length_", "weighted_", "pond_" followed by numbers

  2. Purely numeric column names

  3. General pattern: extracts first number found in each name

  4. Fallback: uses specified fallback strategy if previous methods fail

Examples

Run this code
extract_numeric_values(c("length_8.5", "weighted_10", "pond_12"))
extract_numeric_values(c("8", "10.5", "12"))
extract_numeric_values(c("size_8", "data_10.5", "value_12"))
extract_numeric_values(c("length_8", "no_numbers"), use_fallback = FALSE)
extract_numeric_values(c("bad_name1", "bad_name2"), fallback_type = "ones")

Run the code above in your browser using DataLab