R Factors
R Factors
Home Python Java JavaScript HTML SQL PHP C#
← prev next →
R factors
The factor is a data structure which is used for fields which take only predefined finite
number of values. These are the variable which takes a limited number of different values.
These are the data objects which are used to categorize the data and to store it on multiple
levels. It can store both integers and strings values, and are useful in the column that has a
limited number of unique values.
Factors have labels which are associated with the unique integers stored in it. It contains
predefined set value known as levels and by default R always sorts levels in alphabetical
order.
ADVERTISEMENT
Attributes of a factor
There are the following attributes of a factor in R
1. X
It is the input vector which is to be transformed into a factor.
2. levels
It is an input vector that represents a set of unique values which are taken by x.
3. labels
It is a character vector which corresponds to the number of labels.
4. Exclude
It is used to specify the value which we want to be excluded,
5. ordered
It is a logical attribute which determines if the levels are ordered.
6. nmax
It is used to specify the upper bound for the maximum number of level.
ADVERTISEMENT
How to create a factor?
In R, it is quite simple to create a factor. A factor is created in two steps
R provides factor() function to convert the vector into factor. There is the following syntax of
factor() function
factor_data<- factor(vector)
Example
# Creating a vector as input.
data <- c("Shubham","Nishka","Arpita","Nishka","Shubham","Sumit","Nishka","Shubha
m","Sumit","Arpita","Sumit")
print(data)
print(is.factor(data))
print(factor_data)
print(is.factor(factor_data))
Output
Like vectors, we can access the components of factors. The process of accessing
components of factor is much more similar to the vectors. We can access the element with
the help of the indexing method or using logical vectors. Let's see an example in which we
understand the different-different ways of accessing the components.
Example
Output
[1] Shubham Nishka Arpita Nishka Shubham Sumit Nishka Shubham Sumit
[10] Arpita Sumit
Levels: Arpita Nishka Shubham Sumit
[1] Nishka
Levels: Arpita Nishka Shubham Sumit
[1] Shubham Nishka
Levels: Arpita Nishka Shubham Sumit
[1] Shubham Nishka Arpita Shubham Sumit Nishka Shubham Sumit Arpita
[10] Sumit
Levels: Arpita Nishka Shubham Sumit
Modification of factor
Like data frames, R allows us to modify the factor. We can modify the value of a factor by
simply re-assigning it. In R, we cannot choose values outside of its predefined levels means
we cannot insert value if it's level is not present on it. For this purpose, we have to create a
level of that value, and then we can add it to our factor.
Output
Example
Output
Example
# Apply the factor function with the required order of the level.
new_order_factor<- factor(factor_data,levels = c("Gunjan","Nishka","Arpita","Shubha
m","Sumit"))
print(new_order_factor)
Output
gl(n, k, labels)
Example
gen_factor<- gl(3,5,labels=c("BCA","MCA","B.Tech"))
gen_factor
Output
[1] BCA BCA BCA BCA BCA MCA MCA MCA MCA MCA
[11] B.Tech B.Tech B.Tech B.Tech B.Tech
Levels: BCA MCA B.Tech
← prev next →
Python Java