Function to convert the low and hight levels of a given factor to -1 and 1, respectively.

as.factor2k(x)

Arguments

x

factor to be converted.

Value

the recodified factor with -1 and +1 levels

Examples

library(planex)
library(tidyverse)
#> ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
#>  forcats   1.0.0      stringr   1.5.1
#>  lubridate 1.9.4      tibble    3.2.1
#>  purrr     1.0.4      tidyr     1.3.1
#>  readr     2.1.5     
#> ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
#>  dplyr::filter() masks stats::filter()
#>  dplyr::lag()    masks stats::lag()
#>  Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data(rugosidade)
head(rugosidade)
#>   alimentacao profundidade angulo rugosidade
#> 1          20        0.025     15          9
#> 2          20        0.025     15          7
#> 3          30        0.025     15         10
#> 4          30        0.025     15         12
#> 5          20        0.040     15          9
#> 6          20        0.040     15         11
rugosidade <- mutate(rugosidade,
  alimentacao = as.factor2k(alimentacao),
  profundidade = as.factor2k(profundidade),
  angulo = as.factor2k(angulo)
)
rugosidade
#>    alimentacao profundidade angulo rugosidade
#> 1           -1           -1     -1          9
#> 2           -1           -1     -1          7
#> 3            1           -1     -1         10
#> 4            1           -1     -1         12
#> 5           -1            1     -1          9
#> 6           -1            1     -1         11
#> 7            1            1     -1         12
#> 8            1            1     -1         15
#> 9           -1           -1      1         11
#> 10          -1           -1      1         10
#> 11           1           -1      1         10
#> 12           1           -1      1         13
#> 13          -1            1      1         10
#> 14          -1            1      1          8
#> 15           1            1      1         16
#> 16           1            1      1         14