Recode variables

TODO

  • link to factors

Install required packages

dplyr, forcats

Recode numerical or character variables

Using index vectors

[1] "rot"     "violett" "blau"    "blau"    "orange"  "rot"     "orange" 
[1]   1 200   3 400   5

Using recode() from package dplyr

[1] "rot"     "violett" "blau"    "blau"    "orange"  "rot"     "orange" 
[1] "basic"   "complex" "basic"   "basic"   "complex" "basic"   "complex"

Using ifelse()

 [1]  5  9 10  8  9  3  1 10  9 10  5 10  6  3 10  5  8  7
 [1] "other" "E"     "other" "other" "H"     "C"     "I"     "G"     "A"    
[10] "other" "B"    

Cut continuous variables into categorical variables

Free recoding of value ranges into categories

Two intervals

 [1] "lo" "lo" "hi" "hi" "hi" "hi" "hi" "lo" "lo" "lo" "hi" "hi" "hi" "hi" "lo"
[16] "hi" "hi" "lo" "hi" "lo"

Multiple intervals

 [1] 1 1 3 2 2 3 2 1 1 1 3 2 2 2 1 3 2 1 2 1

Turn ordered value ranges into factor levels using cut()

[1] mid mid hi  mid mid hi 
Levels: lo mid hi
(-Inf,102] (102, Inf] 
        10         10 
[70.5,92.6]  (92.6,102]   (102,108]   (108,127] 
          5           5           5           5 

Recode factors

Change names of factor levels

[1] m f f m m m f f
Levels: f m
[1] male   female female male   male   male   female female
Levels: female male

Using package forcats

[1] M F F M M M F F
Levels: F M

Add, combine and remove factor levels

Add factor levels

[1] hi lo hi
Levels: hi lo
[1] hi   lo   hi   <NA>
Levels: hi lo
[1] hi  lo  hi  mid
Levels: hi lo mid

Using package forcats

[1] hi  lo  hi  mid
Levels: hi lo mid new_level

Combine factor levels

[1] hi    notHi hi    notHi
Levels: hi notHi

Using package forcats

[1] hi    notHi hi    notHi
Levels: hi notHi

Remove factor levels

[1] hi lo
Levels: hi lo mid
[1] hi lo
Levels: hi lo

Using package forcats

[1] hi lo
Levels: hi lo

Reorder factor levels

Using fct_relevel() from package forcats

 [1] A A A A A B B B B B C C C C C
Levels: A B C
 [1] A A A A A B B B B B C C C C C
Levels: B A C
 [1] A A A A A B B B B B C C C C C
Levels: B C A

Reorder group levels according to group statistics

        A         B         C 
 7.800560  4.652087 16.635740 
 [1] A A A A A B B B B B C C C C C
attr(,"scores")
        A         B         C 
 7.800560  4.652087 16.635740 
Levels: B A C

Detach (automatically) loaded packages (if possible)

Get the article source from GitHub

R markdown - markdown - R code - all posts