Skip to contents
  • to_signed(x) converts \(x \in \{0, 1\}\) to \(x' \in \{-1, 1\}\)

  • to_unsigned(x) converts \(x \in \{-1, 1\}\) to \(x' \in \{0, 1\}\)

Usage

to_signed(x)

to_unsigned(x)

Arguments

x

A binary variable

Value

A signed (for to_signed) or unsigned (for to_unsigned) version of x

Examples

# should return `1`
to_signed(0)
#> [1] -1

# should return `1`
to_signed(1)
#> [1] 1

# should return `0`
to_unsigned(-1)
#> [1] 0

# should return `1`
to_unsigned(1)
#> [1] 1

# `to_signed` also works with objects `R` interprets as `0` or `1`
to_signed(10)
#> [1] 1

# `to_unsigned` also works with any signed integer
to_unsigned(-10)
#> [1] 0

# neither function works with factors
try(to_signed(factor(1)))
#> Error in storage.mode(test) <- "logical" : 
#>   invalid to change the storage mode of a factor
tryCatch(to_unsigned(factor(1)), warning=function(w) w)
#> <simpleWarning in Ops.factor(x, 0): ‘>’ not meaningful for factors>