Data Structure - Factor

Q

What is a Factor?

✍: FYIcenter.com

A

A factor is a data structure that stores a vector and its distinct values as categories.

1. Create a factor object with the factor() function -

> f = factor( c("Bike", "Car", "Truck", "Car", "Bike", "Truck") )

> f
[1] Bike  Car   Truck Car   Bike  Truck
Levels: Bike Car Truck

> typeof(f)
[1] "integer"

> is.factor(f)
[1] TRUE

2. Get factor length and members -

> f = factor( c("Bike", "Car", "Truck", "Car", "Bike", "Truck") )

> length(f)
[1] 6

> f[4]
[1] Car
Levels: Bike Car Truck

 

Data Structure - Matrix

Data Structure - List

R Language Basics

⇑⇑ R Software Environment - Frequently Asked Questions

2023-05-31, 311🔥, 0💬