factor คืออะไร?

R เบื้องต้นสำหรับการเงิน

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

หุ้นหรือพันธบัตร?

การลงทุน
stock
bond
bond
stock
R เบื้องต้นสำหรับการเงิน

หุ้นหรือพันธบัตร?

การลงทุน
stock = 2
bond = 1
bond = 1
stock = 2
R เบื้องต้นสำหรับการเงิน

หุ้นหรือพันธบัตร?

การลงทุน
stock
bond
bond
stock
R เบื้องต้นสำหรับการเงิน

การสร้าง factor

answers <- c("stock", "bond", "bond", "stock")
investment <- factor(answers)
investment
stock bond  bond  stock
Levels: bond stock
class(investment)
"factor"
R เบื้องต้นสำหรับการเงิน

การสร้าง factor

as.integer(investment)
2 1 1 2
levels(investment)
"bond"  "stock"
R เบื้องต้นสำหรับการเงิน

ใช้ `cut()` แบ่งกลุ่ม

  • อันดับ: 1-50
  • 1 คือแย่ที่สุด, 50 คือดีที่สุด
head(ranking)
36 45 32 10 42  8
buckets <- c(0, 10, 20, 30, 40, 50)
ranking_grouped <- cut(ranking, breaks = buckets)
head(ranking_grouped)
(30,40] (40,50] (30,40] (0,10]  (40,50] (0,10] 
Levels: (0,10] (10,20] (20,30] (30,40] (40,50]
R เบื้องต้นสำหรับการเงิน

มาฝึกกันเถอะ!

R เบื้องต้นสำหรับการเงิน

Preparing Video For Download...