Open In App

Subset Dataframe Rows Based On Factor Levels in R

Last Updated : 23 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be discussing how to subset a given dataframe rows based on different factor levels with the help of some operators in the R programming language.

Method 1: Subset dataframe Rows Based On One Factor Levels

In this approach to subset dataframe rows based on one-factor levels, the user just needs the in-built operator of the R programming language, for this, the user needs to use the $ and == operators with the data and provide the required details into it specifically to retain the rows where our factor column has one specific factor level in the R programming language.

File Used:

Example: Subset dataframe rows based on one-factor levels

R




data=read.csv('data_gfg.csv')
data=data[data$A == "x", ]
data


Output:

Method 2: Subset dataframe Rows Based On Multiple Factor Levels

In this approach to subset the dataframe rows based on the multiple factor levels, the user simply needs to use the in-built operators of the R programming language, which will be including $,% and a vector which will be containing the required multiple factor levels, as per the requirements of the user to retain the rows where our factor column has multiple specific factor level in the R programming language.

Example: Subset dataframe rows based on multiple factor levels

R




data=read.csv('data_gfg.csv')
data=data[data$A %in% c("x", "z"), ] 
data


Output:


Similar Reads

Get or Set Levels of a Factor in R Programming - levels() Function
levels() function in R Language is used to get or set the levels of a factor. Syntax: levels(x) Parameters: x: Factor Object Example 1: # R program to get levels of a factor # Creating a factor gender <- factor(c("female", "male", "male", "female")); gender # Calling levels() function # to get the levels l
1 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 strings in the encoded factor level vector. In thi
2 min read
Applying User-defined Functions on Factor Levels of Dataset in R Programming - by() Function
by() function in R programming is an object-oriented wrapper function which performs the provided function on factor levels of the data set passed in the arguments of function call. Syntax: by(data, INDICES, FUN) Parameters: data: represents the dataset INDICES: represents the factor list of dataset FUN: represents the function to be performed on f
1 min read
Removing Levels from a Factor in R Programming - droplevels() Function
droplevels() function in R programming used to remove unused levels from a Factor. Syntax: # For vector object droplevels(x, exclude = if(anyNA(levels(x))) NULL else NA, ...) # For data frame object droplevels(x, except, exclude) Parameter values: x represents object from which unused level has to be dropped exclude represents factor levels which s
2 min read
Get the Number of Levels of a Factor in R Programming - nlevels() Function
nlevels() function in R Language is used to get the number of levels of a factor. Syntax: nlevels(x) Parameters: x: Factor Object Example 1: # R program to get the number # of levels of a factor # Creating a factor x <- gl(3, 2) x # Calling nlevels() function # to get the number of levels nlevels(x) Output: [1] 1 1 2 2 3 3 Levels: 1 2 3 [1] 3 Ex
1 min read
How to change the order of levels of a factor in R?
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() fun
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 categorical data. It is used to find the levels of the
1 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 levels() function. Syntax: levels(factor(data)) Exampl
2 min read
Keep Unused Factor Levels in ggplot2 Barplot in R
In this article, we will discuss how to keep unused factor levels in ggplot2 barplot in the R programming language. In this approach to keep unused factor levels in ggplot2 barplot, the user first needs to install and import the ggplot2 package in the R console and plot the barplot of the data consisting of zero values with the geom_bar() and ggplo
2 min read
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 the factor input vectorlevels are the names to be s
1 min read