Building Cloud Expertise with centron - Our Tutorials
Whether you are a beginner or an experienced professional, our practical tutorials provide you with the knowledge you need to make the most of our cloud services.
Understanding the which() Function in R
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.
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) #Position 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
...
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
...
[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
...
[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!!! Understanding the which() Function in R
Create a Free Account
Register now and gain exclusive access to advanced resources, personalized support, and a community of experts.
Recent posts
Unlock the Full Potential of R with Our Cloud - Start Your Free Trial Today!
Step into the future of data analysis with our advanced cloud solutions, specifically optimized for R and its powerful functions like the which() function. Experience unmatched speed, reliability, and scalability that transform the way you handle data. Don't miss out on elevating your R projects - sign up for our free trial now and witness the difference firsthand!