Skip to contents

This function takes a list of numeric vectors and returns a new list where each numeric element is automatically converted to either an integer (if it is a whole number) or kept as a float (numeric). It also preserves the original names of vector elements, if any.

Usage

convert_numeric_types(data)

Arguments

data

A list of numeric vectors.

Value

A list of the same structure where each numeric element is coerced to the appropriate type: integers for whole numbers, and floats otherwise. Names are preserved.

Examples

input_list <- list(
  a = c(x = 1.0, y = 2.5, z = 3.0),
  b = c(foo = 4.0, bar = 5.1),
  c = c(6, 7, 8)
)
convert_numeric_types(input_list)
#> $a
#>   x   y   z 
#> 1.0 2.5 3.0 
#> 
#> $b
#> foo bar 
#> 4.0 5.1 
#> 
#> $c
#> [1] 6 7 8
#>