R Factor

factors are vector-like objects used to store sets of categorical data.

factors is like a box with several labelled levels!

e.g. stationery<factor(c(“Glue”,“Scissors”,“Pens”)

Here, we have constructed a vector of class character, and converted it to a factor. The factor is displayed without quotes. It is informative to examine how the factor is stored, using unclass(drinks)

Refer to individual elements in the same way as a vector.

The classes available for vectors are:

Example:

A teacher wants to record the best student for each month, from January to November.

Instead of using papers and memo notes, he is thinking to use R studio to create a chart.


# Step 1-Creating a vector.  
vector1<-c("Sally","Nancy","Alice","Nancy","Sally","Tom","Nancy","Sally","Tom","Alice","Tom")  
  
print(vector1)  

 [1] "Sally" "Nancy" "Alice" "Nancy" "Sally" "Tom"   "Nancy" "Sally" "Tom"  
[10] "Alice" "Tom"  

print(is.factor(vector1)) #checking

[1] FALSE
# Step 2-Convert vector to factor  
factor1<- factor(vector1)  
  
print(factor1) 

 [1] Sally Nancy Alice Nancy Sally Tom   Nancy Sally Tom   Alice Tom  
Levels: Alice Nancy Sally Tom

print(is.factor(factor1))  #checking
 [1] TRUE

The teacher wants to cross-check some of the records.


#Step 3-Checking an element inside a factor

#Printing all elements of factor  
print(factor1)  

[1] Sally Nancy Alice Nancy Sally Tom   Nancy Sally Tom   Alice Tom  
Levels: Alice Nancy Sally Tom

#Accessing 4th element of factor  
print(factor1[4])  
[1] Nancy
Levels: Alice Nancy Sally Tom

#Accessing 1st and 2nd element  
print(factor1[c(1,2)])  

[1] Sally Nancy
Levels: Alice Nancy Sally Tom
  
#Accessing all element except 1st one  
print(factor1[-1])  

 [1] Nancy Alice Nancy Sally Tom   Nancy Sally Tom   Alice Tom  
Levels: Alice Nancy Sally Tom

The teacher recorded the first 11 months and now has to complete the last month.


#Step 4-Add a new element is not possible

factor_data[12] <-"Hsin Wei"  

Error in factor_data[12] <- "Hsin Wei" : object 'factor_data' not found

print(factor1)  #No changes made

[1] Sally Nancy Alice Nancy Sally Tom   Nancy Sally Tom   Alice Tom  
Levels: Alice Nancy Sally Tom
#Add a new level before adding a new element 

levels(factor1) <- c(levels(factor1),"Hsin Wei") 
 
factor1[12] <- "Hsin Wei"  

print(factor1)

1] Sally    Nancy    Alice    Nancy    Sally    Tom      Nancy    Sally   
 [9] Tom      Alice    Tom      Hsin Wei
Levels: Alice Nancy Sally Tom Hsin Wei

References

https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/factor