base2dec: Convert a string of numeric digits from some base into...

View source: R/num_fun.R

base2decR Documentation

Convert a string of numeric digits from some base into decimal notation.

Description

base2dec converts a sequence of numeric symbols (digits) from some base notation to decimal (i.e., base 10) notation.

Usage

base2dec(x, base = 2)

Arguments

x

A (required) sequence of numeric symbols (as a sequence or vector of digits).

base

The base of the symbols in seq. Default: base = 2 (binary).

Details

Individual digits (e.g., 0-9) must exist in the specified base (i.e., every digit value must be lower than the base value).

base2dec is the complement of dec2base.

Value

An integer number (in decimal notation).

See Also

dec2base converts decimal numbers into another base; as.roman converts integers into Roman numerals.

Other numeric functions: dec2base()

Examples

# (a) single string input:
base2dec("11")   # default base = 2
base2dec("0101")
base2dec("1010")

base2dec("11", base = 3)
base2dec("11", base = 5)
base2dec("11", base = 10)
 
# (b) numeric vectors as inputs:
base2dec(c(0, 1, 0))
base2dec(c(0, 1, 0), base = 3)

# (c) character vector as inputs:
base2dec(c("0", "1", "0"))
base2dec(c("0", "1", "0"), base = 3)

# (d) multi-digit vectors:
base2dec(c(1, 1))
base2dec(c(1, 1), base = 3)

# Special cases:
base2dec(NA)
base2dec(0)
base2dec(c(3, 3), base = 3)  # Note message!

# Note: 
base2dec(dec2base(012340, base = 5), base = 5)
dec2base(base2dec(043210, base = 5), base = 5)


hneth/i2ds documentation built on Jan. 25, 2024, 2:22 p.m.