Ordenen en subsetten van factoren

Introductie tot R voor Financiën

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

Ordenen

  • Low < Medium < High

ch4_vid2_slides.004.png

Introductie tot R voor Financiën

Hoe ordenen?

rank <- c("low", "medium", "low", "medium", "high")

rank_wrong_order <- ordered(rank)

rank_wrong_order
low    medium low    medium high  
Levels: high < low < medium
Introductie tot R voor Financiën

Hoe ordenen?

rank_order <- factor(rank, ordered = TRUE,
                     levels = c("low", "medium", "high"))
rank_order
low    medium low    medium high  
Levels: low < medium < high
summary(rank_order)
low medium   high 
  2      2      1
Introductie tot R voor Financiën

Subsets van factoren

# Alleen low
rank_order[c(1,3)]
low low
Levels: low < medium < high
summary(rank_order[c(1,3)])
low medium   high 
  2      0      0
Introductie tot R voor Financiën

Subsets van factoren

# Alleen low; levels medium en high verwijderen
rank_order[c(1,3), drop = TRUE]
low low
Levels: low
summary(rank_order[c(1,3), drop = TRUE])
low 
  2 
Introductie tot R voor Financiën

Laten we oefenen!

Introductie tot R voor Financiën

Preparing Video For Download...