The which() function in R programming

Filed Under: R Programming
THE WHICH FUNCTION IN R PROGRAMMING LANGUAGE

The which() function in R returns the position or the index of the value which satisfies the given condition. The Which() function in R gives you the position of the value in a logical vector. The position can be of anything like rows, columns and even vector as well.

Syntax of which() function in R

which(): The which function in R returns the position of the values in the logical vector.

which(x,arr.ind = F,useNames = F)

Where,

  • X = An input logical vector.
  • Arr.ind = Returns the array indices if x is an array.
  • useNames = Indicates the dimension names of an array.

A simple example of which() function

Well, you got the definition of which function along with its working nature. Now, let’s apply our learned things practically.

Let’s see how it works.

which(letters=="p")

16

which(letters=="n")

14

which(letters=="l")

12

“lettters” is a built-in constant with all the 26 letters of the English alphabet arranged serially.

The outputs that you see above, are representative of the position of each letter in the data frame. As you can see, the letter “p” is 16th in the alphabet, while “l” and “n” are 14th and 12th respectively.

Let’s now learn how to work with the which function.

Using the which() function with vectors

Now, let’s create a vector in R language and and then by using the which function, let’s do the position tracking.

#creating a vector 
df<- c(5,4,3,2,1)
#Postion of 5
which(df==5)

1

#Position of 1
which(df==1)

5

#Position of values greater than 2
which(df>2)

1 2 3

Great! The which() in R returns the position of the values in the given input. You can also use the function to pass specific conditions and get the positions of the output that match the conditions as we saw in the last example.

Using the which() function with dataframes

Now, let’s see how we can apply the which function with respect to the data frame in R langauge.

df<-datasets::BOD
df
    Time  demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8

For this purpose, we are using the BOD dataset which includes 2 columns namely, Time and Demand. It’s yet another built-in dataset.

Let’s use the which function and try to find the position of the values in the data.

which(df$demand=='10.3')
2

You can also input a list of values to the which() function. Look at the example below where I am trying to find the position of two values from the dataframe.

which(df$demand==c(8.3,16.0))
1    4

Find columns in a data frame with numeric values using which()

You can even use the which() function to find the column names in a data which contains numerical data.

Let’s see how it works in R language. For this, we are using the “Iris” dataset.

df<-datasets::iris
df
   Sepal.Length  Sepal.Width   Petal.Length   Petal.Width  Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            4.6         3.1          1.5         0.2     setosa
5            5.0         3.6          1.4         0.2     setosa
6            5.4         3.9          1.7         0.4     setosa
7            4.6         3.4          1.4         0.3     setosa
8            5.0         3.4          1.5         0.2     setosa
9            4.4         2.9          1.4         0.2     setosa
10           4.9         3.1          1.5         0.1     setosa
11           5.4         3.7          1.5         0.2     setosa
test<-which(sapply(df,is.numeric))
colnames(df)[test]
"Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 

The output shows that the iris dataset has 5 columns in it. Among them, 4 are numerical columns and 1 is categorical (Species).

We’ve used the sapply function in R along with the which() method here.

The which() function has returned only the names of numerical columns as per the input condition. If you a data analyst, then which() function will be invaluable for you.

Which function with Matrix in R

Finally, we have arrived at the matrix in R. Well, you can use the which() function in R language to get the position of the values in a matrix. You will also get to know about the arr.index parameter in this section.

First things first – Create a matrix

df<-matrix(rep(c(1,0,1),4),nrow = 4)
df
       [,1] [,2] [,3]
[1,]    1    0    1
[2,]    0    1    1
[3,]    1    1    0
[4,]    1    0    1

Fantastic!!! You have just created a good looking matrix. Kudos. Let’s use the which() to get the position of the value ‘0’ in our matrix.

which(df==0,arr.ind = T)
      row col
[1,]   2   1
[2,]   1   2
[3,]   4   2
[4,]   3   3

Well, the which function has returned the positions of the value ‘0’ in the matrix.

The first occurrence of “0” is in the second row first column. Then the next occurrence is in the first row, second column. Then we have 4th row, second column. And finally, the third row, third column.

Conclusion

The which() function in R is one of the most widely used function in data analysis and mining.

The function gives the position of the values in the data. If you are working with tons of data, then it will be hard to find specific values position in that and there comes which() in R.

That’s all for now. Happy positioning!!!

More read: R documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages