c("car", "DescTools")
wants <- wants %in% rownames(installed.packages())
has <-if(any(!has)) install.packages(wants[!has])
c("red", "purple", "blue", "blue", "orange", "red", "orange")
myColors <- character(length(myColors))
farben <-== "red"] <- "rot"
farben[myColors == "purple"] <- "violett"
farben[myColors == "blue"] <- "blau"
farben[myColors == "orange"] <- "orange"
farben[myColors farben
[1] "rot" "violett" "blau" "blau" "orange" "rot" "orange"
replace(c(1, 2, 3, 4, 5), list=c(2, 4), values=c(200, 400))
[1] 1 200 3 400 5
recode()
from package car
library(car)
recode(myColors, "'red'='rot'; 'blue'='blau'; 'purple'='violett'")
[1] "rot" "violett" "blau" "blau" "orange" "rot" "orange"
recode(myColors, "c('red', 'blue')='basic'; else='complex'")
[1] "basic" "complex" "basic" "basic" "complex" "basic" "complex"
ifelse()
c(5, 9, 11, 8, 9, 3, 1, 13, 9, 12, 5, 12, 6, 3, 17, 5, 8, 7)
orgVec <- 10
cutoff <- ifelse(orgVec <= cutoff, orgVec, cutoff)) (reVec <-
[1] 5 9 10 8 9 3 1 10 9 10 5 10 6 3 10 5 8 7
c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K")
targetSet <- c("Z", "E", "O", "W", "H", "C", "I", "G", "A", "O", "B")
response <- ifelse(response %in% targetSet, response, "other")) (respRec <-
[1] "other" "E" "other" "other" "H" "C" "I" "G"
[9] "A" "other" "B"
set.seed(123)
rnorm(20, mean=100, sd=15)
IQ <-ifelse(IQ >= 100, "hi", "lo")
[1] "lo" "lo" "hi" "hi" "hi" "hi" "hi" "lo" "lo" "lo" "hi" "hi" "hi" "hi"
[15] "lo" "hi" "hi" "lo" "hi" "lo"
library(car)
recode(IQ, "0:100=1; 101:115=2; else=3")
[1] 1 1 3 2 2 3 2 1 1 1 3 2 2 2 1 3 2 1 2 1
cut()
cut(IQ, breaks=c(0, 85, 115, Inf), labels=c("lo", "mid", "hi"))
IQfac <-head(IQfac)
[1] mid mid hi mid mid hi
Levels: lo mid hi
cut(IQ, breaks=c(-Inf, median(IQ), Inf))
medSplit <-summary(medSplit)
(-Inf,102] (102, Inf]
10 10
cut(IQ, quantile(IQ), include.lowest=TRUE)
IQdiscr <-summary(IQdiscr)
[70.5,92.6] (92.6,102] (102,108] (108,127]
5 5 5 5
factor(c("hi", "lo", "hi"))) (status <-
[1] hi lo hi
Levels: hi lo
4] <- "mid"
status[ status
[1] hi lo hi <NA>
Levels: hi lo
levels(status) <- c(levels(status), "mid")
4] <- "mid"
status[ status
[1] hi lo hi mid
Levels: hi lo mid
status
hiNotHi <-levels(hiNotHi) <- list(hi="hi", notHi=c("mid", "lo"))
hiNotHi
[1] hi notHi hi notHi
Levels: hi notHi
library(car)
recode(status, "'hi'='high'; c('mid', 'lo')='notHigh'")) (statNew <-
[1] high notHigh high notHigh
Levels: high notHigh
1:2] status[
[1] hi lo
Levels: hi lo mid
droplevels(status[1:2])) (newStatus <-
[1] hi lo
Levels: hi lo
reorder.factor()
from package DescTools
factor(rep(LETTERS[1:3], each=5))) (facGrp <-
[1] A A A A A B B B B B C C C C C
Levels: A B C
library(DescTools)
reorder.factor(facGrp, new.order=c("C", "B", "A"))) (facRe <-
[1] A A A A A B B B B B C C C C C
Levels: C B A
rnorm(15, rep(c(10, 5, 15), each=5), 3)
vec <-tapply(vec, facGrp, FUN=mean)
A B C
7.800560 4.652087 16.635740
reorder(facGrp, vec, FUN=mean)
[1] A A A A A B B B B B C C C C C
Levels: B A C
try(detach(package:car))
try(detach(package:DescTools))
R markdown - markdown - R code - all posts