Analysis in R: Manipulating Factor! About Factor


When manipulating data in R, you may create a vector or data frame data whose class is factor. If you are not familiar with the operation, handling factors, called factors, can be tricky. Therefore, we will introduce commands related to factor. If you are familiar with factor, it is very useful, for example, when creating a graph, the order of axes are not interchanged, etc. When you are manipulating data in R and an error occurs, checking with class (data) is a shortcut to solving the problem.

Sponsored Link


See the command and package help for details.

#Creating Data Examples
Data <- LETTERS[c(5, 9, 12, 1, 4, 9, 22, 4, 5)]

#Factorization of data, where level is a reference sequence
LT <- factor(Data , levels = LETTERS)

[1] E I L A D I V D E
Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y 

#Check the order of the strings in the sample data
[1]  5  9 12  1  4  9 22  4  5

#Simplified reference sequence display with sample data
[1] E I L A D I V D E
Levels: A D E I L V

#Delete unused levels
LT[, drop = TRUE]
[1] E I L A D I V D E
Levels: A D E I L V

#Change level
levels(LT) <- letters

[1] e i l a d i v d e
Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z

I hope this makes your analysis a little easier !!