wants <- c("dplyr", "forcats")
has <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])
myColors <- c("red", "purple", "blue", "blue", "orange", "red", "orange")
farben <- character(length(myColors))
farben[myColors == "red"] <- "rot"
farben[myColors == "purple"] <- "violett"
farben[myColors == "blue"] <- "blau"
farben[myColors == "orange"] <- "orange"
farben
[1] "rot" "violett" "blau" "blau" "orange" "rot" "orange"
[1] 1 200 3 400 5
recode()
from package dplyr
[1] "rot" "violett" "blau" "blau" "orange" "rot" "orange"
[1] "basic" "complex" "basic" "basic" "complex" "basic" "complex"
ifelse()
orgVec <- c(5, 9, 11, 8, 9, 3, 1, 13, 9, 12, 5, 12, 6, 3, 17, 5, 8, 7)
cutoff <- 10
(reVec <- ifelse(orgVec <= cutoff, orgVec, cutoff))
[1] 5 9 10 8 9 3 1 10 9 10 5 10 6 3 10 5 8 7
targetSet <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K")
response <- c("Z", "E", "O", "W", "H", "C", "I", "G", "A", "O", "B")
(respRec <- ifelse(response %in% targetSet, response, "other"))
[1] "other" "E" "other" "other" "H" "C" "I" "G" "A"
[10] "other" "B"
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
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
[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
[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
[1] hi notHi hi notHi
Levels: hi notHi
Using package forcats
[1] hi notHi hi notHi
Levels: hi notHi
[1] hi lo
Levels: hi lo mid
[1] hi lo
Levels: hi lo
Using package forcats
[1] hi lo
Levels: hi lo
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
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
R markdown - markdown - R code - all posts