![]() |
Data manipulation and transformation require the use of data manipulation verbs and the dplyr package in R is crucial. One of its functions is filter(), which allows the row to be selected based on imposed conditions. However, one of the activities that frequently occur in data analysis processing is data filtering based on a variable with changing values. In this article, we will explore how to use variables within the dplyr in the R Programming Language. Understanding dplyr::filter()The filter() function is part of the dplyr package and is used to select rows from a data frame based on specified conditions. Here’s the basic syntax:
Basics of dplyr::filter()Before proceeding to work with variables, it is critical to recall how the filter() function is used at a basic level. The filter() function is also taken from the dplyr package which is highly popular in data manipulation processes and it helps to select rows in the data frame based on the defined conditions.
Output: id value
1 1 10
2 2 20
3 3 30
4 4 40
5 5 50
id value
1 3 30
2 4 40
3 5 50
Using Variables in filter()To filter data based on certain variables one has to know how to evaluate this particular variable in a filter() function. The !! (bang-bang) operator from the rlang package helps in unquoting the variable so that it can be evaluated.
Output: id value
1 1 10
2 2 20
3 3 30
4 4 40
5 5 50
id value
1 3 30
2 4 40
3 5 50
Example with Multiple VariablesYou can also use multiple variables to create complex filtering conditions. Here’s an example:
Output: id value category
1 1 10 A
2 2 20 B
3 3 30 A
4 4 40 B
5 5 50 A
id value category
1 3 30 A
2 5 50 A
Using Variables with Different Data TypesFiltering based on different data types (e.g., strings, numeric) requires ensuring the variable is evaluated correctly. Here’s an example with a string variable:
Output: id value category
1 1 10 A
2 2 20 B
3 3 30 A
4 4 40 B
5 5 50 A
id value category
1 2 20 B
2 4 40 B
ConclusionUsing variables within the dplyr::filter() function is one of the most versatile and convenient tools to make your data processing code easily transformable and reusable. By leveraging the !! If you use the operator from the rlang package, you can easily turn variables into your filter conditions. This technique provides to maximize and minimize the dataset manipulating flexibility to make your code look better and able to apply for other datasets and analysis. |
Reffered: https://www.geeksforgeeks.org
R Language |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 20 |