What is the which() Function in the R Programming Language in Ubuntu 20.04?
In R, the which() function returns the index or position of the value that meets the provided criterion. The Which() function returns the value’s position in a logical vector in R. The location can be in the form of rows, columns, or even a vector. The which() function, unlike some of the other base R methods, will only take inputs with the type of as logical, whereas the others will return an error.
Syntax of the which() Function in the R Programming Language in Ubuntu 20.04?
The placement of the elements in the logical vector is returned by the which function in R. So here is the general syntax of the which function we generally use in the R language.
which(x,arr.ind = F, useNames = T) arrayInd(ind, .dim, .dimnames = NULL, useNames = TRUE)
Some arguments are required for the which function. We have discussed below each argument’s functionality in R.
x: x is a logical array or vector. NAs are acceptable but not required (as if they were FALSE).
arr.ind: When x is an array, should the array indices be returned?
ind: A vector of integer-valued index values, as a result of which (x).
dim(.): integer vector.dim
.dimnames: It is a set of character dimnames that is optional (.). If true, useNames will be utilised to build dimnames using arrayInd() (and therefore, which(*, arr.ind=TRUE)). These will be utilized as column names if names(.dimnames) are not empty. Row names are.dimnames[].
useNames: It is a logical parameter that indicates whether or not the arrayInd() value should have (non-null) dimnames.
How Does the which() Function Work in the R in Ubuntu 20.04?
So now you understand which() function and how it operates. Let’s have a glimpse. This article shows you how which() function performs in practice with various examples.
The which() function returns the alphabetical placement of the provided letter in the example below. For example, as A is the first capital letter, 1 is returned, and because Z is the last capital letter, 26 is returned.
Inside the R prompt, we have called the which function. The which function takes random alphabets respectively. From this which function, we will get the position of the specified letters alphabetically. As “letters” is a built-in constant that contains all 26 characters of the English alphabet in sequential order. The above outputs indicate the location of each character. As you can see, “s” is the 19th letter in the alphabet, followed by other letters which also generate their position, respectively.
The position of the input elements is returned by the which() method. As we have seen in the last example, you can also specify requirements for the function and retrieve the output places that match those conditions. The which() function is used to return the positions of some items of the defined vector in the example below.
The vector is initialized with some positive integers and kept inside the variable vector. The which function is utilized here and takes the vector inside it as an argument with the specific condition. The which function is getting the position value of the provided integers in the above vector. The output generated from each which() function is the index position of the elements present in the given vector.
The which() function is deployed to locate the columns inside a data frame having numeric values in the example below. An Iris data set is a data frame with four columns of numerical values and one column of category values (Species). The which() function returns the names of the columns in the data set that have numeric values.
We have specified the iris dataset to the variable df. The df is called inside the head function, which displays the first few entries of the iris dataset corresponding to each column as we came to know that the iris dataset contains four numerical columns and one categorical column. Now, in another variable, “Result”, we have called the which function where the sapply() method is also deployed.
To the sapply() method, we have provided the dataset variable df and the “is.numeric” argument. According to the specified criterion, the which() function returned only numerical column names.
Finally, we’ve reached the R matrix. In R, you may get the location of the elements in a matrix by using the which() function. This section also covers the arr.index argument. The which() method is used in the example below to determine the location of an item in a matrix. In this case, the value 2’s location in the supplied matrix is determined.
Here, we have constructed the three columns and the four-row matrix with the matrix function. Then, printed the values of the full matrix. To identify the location of element 2 according to the above matrix, we have used the which() method. The arr.ind argument is set to TRUE for this which() function. The occurrence of the value 2 in the given matrix is generated.
That’s all about the which function in R. The which() function will come in handy if you’re a data analyst. In the analysis of data and mining, the R function which() is among the most extensively utilized. The location of the elements in the data is provided by this function. When dealing with a large amount of data, it might be difficult to locate certain values; that is where R’s which() function comes in. We have covered some instances of which function will clear you more about which function.