Sắp xếp và trích xuất factor

Giới thiệu R cho Tài chính

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

Sắp thứ tự

  • Thấp < Trung bình < Cao

ch4_vid2_slides.004.png

Giới thiệu R cho Tài chính

Sắp thế nào?

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

rank_wrong_order <- ordered(rank)

rank_wrong_order
low    medium low    medium high  
Levels: high < low < medium
Giới thiệu R cho Tài chính

Sắp thế nào?

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
Giới thiệu R cho Tài chính

Tập con của factor

# Chỉ low
rank_order[c(1,3)]
low low
Levels: low < medium < high
summary(rank_order[c(1,3)])
low medium   high 
  2      0      0
Giới thiệu R cho Tài chính

Tập con của factor

# Chỉ low, loại cấp medium và high
rank_order[c(1,3), drop = TRUE]
low low
Levels: low
summary(rank_order[c(1,3), drop = TRUE])
low 
  2 
Giới thiệu R cho Tài chính

Hãy luyện tập!

Giới thiệu R cho Tài chính

Preparing Video For Download...