Tutorials  /  JavaScript

Understanding the which() Function in R

Ccentron Redaktion · January 2024 ·5 min read ·JavaScript, 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.

Syntax of which() Function in R

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

Code
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.
VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

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.

Code
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.

Code
#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.

Code
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.

Code
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.

Code
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.

Code
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

Code
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.

Code
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

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren