On this page, I’ll illustrate how to return index positions of certain values using the which function in R.
Table of contents:
You are viewing: Which R
Let’s do this:
Definition & Basic R Syntax of which Function
Definition: The which R function returns the indices of certain values based on a logical condition.
Basic R Syntax: Please find the basic R programming syntax of the which function below.
In the following, I’ll show five examples for the application of the which function in R.
Example 1: Basic Application of which Function
Example 1 illustrates how to use the which() function in the R programming language. First, we have to create some example data:
Read more : Which Gatsby Character Are You
You can see based on the previous output of the RStudio console that our example data is a vector containing five numeric values.
Let’s assume that we want to get the index positions of the value 4. Then, we can apply the which function to our vector as shown below:
The which function returns the values 3 and 5, i.e. the third and the fifth element of our example vector contains the value 4.
Example 2: Applying which Function with Multiple Logical Conditions
The following R syntax explains how to use which() with more than one logical condition. Have a look at the following R syntax:
The previous R code returned each vector index position of elements that are either equal to 4 or equal to 1. We were able to return this result by using the |-operator.
Example 3: Count Occurrences Using which & length Functions
In this Example, I’ll show how to use the which function in combination with the length function to count the number of occurrences of certain values in R. For this, we simply need to wrap the length function around our previously used R code:
The RStudio console returns 3, i.e. the values 4 and 1 appear three times in our example vector.
Example 4: Subsetting Data Frame Rows Using which Function
Read more : Which Of The Following Is A Characteristic Of Customer-driven Marketing
The which command can also be used to subset data tables in R. In this Example, I’ll explain how to extract or remove certain data frame rows using the which function. First, we have to create some example data:
Our data matrix contains five rows and three columns. Now, we can apply the which function to select and retain only specific rows of our data:
As you can see, we have extracted only rows were the variable x1 has a value larger than or equal to 3.
Example 5: Subsetting Data Frame Columns Using which & colnames Functions
We can also use the which function to extract certain columns of our data frame. In this case, we are using the colnames function and the %in%-operator to create our logical object:
The previous R code returned a new data frame containing of the variables x1 and x3 (i.e. x2 was removed).
Video & Further Resources
Do you need more explanations on the content of this tutorial? Then you may have a look at the following video of my YouTube channel. In the video, I’m explaining the R codes of this article in RStudio.
Furthermore, you might want to have a look at the related articles on this homepage. I have published numerous other articles already.
- Extract Row from Data Frame
- Extract Certain Columns of Data Frame
- Extract data.table Column as Vector Using Index Position
- Get Column Index in Data Frame by Variable Name
- Find Index of Maximum & Minimum Value of Vector & Data Frame Row
- Find Index of Element in Vector
- R Functions List (+ Examples)
- The R Programming Language
In this R programming tutorial you learned how to give the TRUE indices of a logical object. Please let me know in the comments section below, if you have any additional questions.
Source: https://t-tees.com
Category: WHICH