Which R

On this page, I’ll illustrate how to return index positions of certain values using the which function in R.

Table of contents:

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:

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:

Refer to more articles:  Which Of The Following Is True Of Disability Insurance

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

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.

Refer to more articles:  Which One Of The Following Are Carbohydrates Proteins And Fats

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.

Related Posts

Which Is True About Scientific Inquiry

One thing is common to all forms of science: an ultimate goal “to know.” Curiosity and inquiry are the driving forces for the development of science. Scientists…

Which Zodiac Sign Is The Oldest

Which Zodiac Sign Is The Oldest

The Ancient Greeks — along with other civilizations of the time — widely believed in a now-iconic phrase: “As Above, So Below.” In other words, the Greeks…

Which Of The Following Is A Characteristic Of Beta

What Is Beta? Beta is a measure of a stock’s volatility in relation to the overall market. By definition, the market, such as the S&P 500 Index,…

Which Is Better Graphite Or Fiberglass Pickleball Paddle

Fiberglass vs Graphite Pickleball Paddle The pickleball arena resonates with the constant buzz of energetic gameplay and the clink of paddles. Among the myriad of considerations for…

Which Of The Following Best Describes The Paintbox Era

The Hay Wain, Study Artist: John ConstableYou may be interested Which Css Property Configures The Font Typeface Which Chacos Should I Get Which Is Not A Power…

Which Of The Following Is A Transition Element

Which Of The Following Is A Transition Element

Table of Content What are Transition Elements?Electronic Configuration of Transition ElementsGeneral Properties of Transition ElementsAtomic Ionic RadiiIonization EnthalpyFrequently Asked QuestionsYou may be interested Which Of The Following…