How to change the order of levels of a factor in R? Last Updated : 23 May, 2021 Comments Improve Suggest changes Like Article Like Report In R programming language, factors are used to represent categorical data by uniquely identifying the elements from the given vector. It will return the levels of the unique elements when factor function is applied. In this article we are going to discuss how to change the levels of the factor. We can get the levels of the vector using factor() function itself Syntax: factor(vector) Parameters: vector: vector with categorical datalevels(): specifies the levels Returns: vector elements with levels If we want to change the order of the levels, we can specify the levels parameter in the factor() function Syntax: factor(vector,levels=c(elements)) levels parameter can accept the vector elements and within this, the order of levels can be passed as a vector. Given below are various implementations for this approach: Example: R # Create a vector with elements data =c("bobby", "sravan", "sravan", "pinkey", "rohith","rohith") #apply factor to vector to get # unique data data=factor(data) print(data) #change the order of the levels # from the resultant data ordered_data=factor(data,levels=c( 'pinkey','rohith','sravan','bobby')) print(ordered_data) Output: Example 2: R # Create a vector with elements data =c(1,2,3,4,5.7,8.9,8.0,1,2,3) # apply factor to vector to get # unique data data=factor(data) print(data) # change the order of the levels # from the resultant data ordered_data=factor(data,levels=c( 5.7,2,3,4,1,8,8.9)) print(ordered_data) Output: Comment More infoAdvertise with us Next Article How to change the order of levels of a factor in R? gottumukkalabobby Follow Improve Article Tags : R Language R Programs R-Factors R Factor-Programs Similar Reads Reorder Levels of Factor without Changing Order of Values in R In this article, we will discuss how to Reorder Levels of Factor without Changing the Order of Values in R programming language. If we want to change the order of the levels, we can specify the levels() parameter in the factor() function Syntax: factor(actual_vector,levels) Where, actual_vector is t 1 min read How to Reorder Factor Levels in R? In this article, we will be looking at the approach to reorder factor levels using functions in R Programming language. Using factor() function to reorder factor levels is the simplest way to reorder the levels of the factors, as here the user needs to call the factor function with the factor level 2 min read How to set level of a factor column in R dataframe to NA? A data frame may contain columns belonging to different classes. A factor type column is a set of categorical variables each mapped to a unique level. The levels give us idea about the co-morbidity of the data variable with each other. These levels can be modified both quantitatively (increase/decre 5 min read How to convert factor levels to list in R ? In this article, we are going to discuss how to convert the factor levels to list data structure in R Programming Language. We can get the levels of the vector using factor() function Syntax: factor(vector) Return type: vector elements with levels. If we want to get only levels, Then we can use leve 2 min read Find the levels of factor of a given vector in R Factors are the objects which are used to categorize the data and display the data as levels. The objects can be integer, character. They can be used to find the unique values in the given vector. The resulting data is known as levels. Factors are useful in statistical analysis in analyzing the cate 1 min read How to count values per level in a factor in R In this article, we will discuss how to count the values per level in a given factor in R Programming Language. Method 1 : Using summary() method summary() method in base R is a generic function used to produce result summaries of the results of the functions computed based on the class of the argum 5 min read How to reverse the order of a dataframe in R? Order reversal of the dataframe involves swapping of the rows or columns in such a way, that the elements are accessed from backward. In this article, we will discuss various ways of reversing the order of a dataframe both column and row-wise. Reversing the order of columns Method 1: Using the rev m 5 min read Get All Factor Levels of DataFrame Column in R The data frame columns in R can be factorized on the basis of its factor columns. The data frame factor columns are composed of factor levels. Factors are used to represent categorical data. Each of the factor is denoted by a level, computed in the lexicographic order of appearance of characters or 3 min read How to Convert Character to Factor in R? The as.factor() method in R Programming Language is used to convert the character vector to factor class. Converting Character Vector To Factor Syntax: as.factor(char-vec) where char-vec is the character vector The class indicative of the data type of the vector can be obtained using the class() me 2 min read How to Convert Factor to Character in R? In this article, we will discuss how to convert the Factor Class to the character Class in the R Programming Language. Method 1: Convert a Single Factor Vector to Character Vector To convert a single factor vector to a character vector we use the as.character() function of the R Language and pass th 4 min read Like