While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
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.
which(): The which function in R returns the position of the values in the logical vector.
which(x,arr.ind = F,useNames = F)
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.
“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.
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)
#Position of 1 which(df==1)
#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.
Now, let’s see how we can apply the which function with respect to the data frame in R langauge.
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.
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.
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.
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
"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.
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.
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
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Click here to Sign up and get $200 of credit to try our products over 60 days!