📌  相关文章
📜  R 中的 which()函数

📅  最后修改于: 2022-05-13 01:54:41.382000             🧑  作者: Mango

R 中的 which()函数

R 编程语言中的which()函数用于返回指定值在逻辑向量中的位置。

示例 1:Which()函数适用 字母表

在下面的例子中,which()函数返回指定字母的字母位置。例如,a 是字母序列中的第一个字母,这就是为什么返回 1 而 z 是序列中的最后一个字母,因此返回 26。



R
# R program to illustrate
# which() function
  
# Calling the which function
# to return alphabetical position
# of the given alphabet
which(letters == "a")
which(letters == "d")
which(letters == "z")
which(letters == "p")
which(letters == "g")


R
# R program to illustrate
# which() function
  
# Creating a vector of some elements
vector <- c(3, 5, 1, 6, 12, 4)
  
# Getting the position of element 12
# in the above vector
which(vector == 12)
  
# Getting the position of element 1
# in the above vector
which(vector == 1)
  
# Getting the position of element 6
# in the above vector
which(vector == 6)
  
# Getting the position of elements
# those are greater than 5
which(vector > 5)


R
# Considering “Iris” dataset
data_set <- datasets::iris
  
# Printing the Iris dataset values
# along with its 5 columns out of which
# 4 columns are numerical and 1 is categorical
# (Species)
head(data_set)
  
# Calling the which() function over
# the above specified data set that 
# returns the columns with numeric values
Result <- which(sapply(data_set, is.numeric))
  
# Printing the columns with numeric values
colnames(data_set)[Result]


R
# Creating a matrix of 3 columns and 4 rows
Matrix <- matrix(rep(c(1, 2, 3), 4), nrow = 4)
  
# Printing the entire matrix with its values
Matrix
  
# Calling the which() function to find the 
# position of value 2 in the above matrix
which(Matrix == 2, arr.ind = T)


输出 :

[1] 1
[1] 4
[1] 26
[1] 16
[1] 7

示例 2:带有向量的 which()函数

在下面的示例中,返回指定向量的某些元素的位置,这在 which()函数的帮助下。

电阻

# R program to illustrate
# which() function
  
# Creating a vector of some elements
vector <- c(3, 5, 1, 6, 12, 4)
  
# Getting the position of element 12
# in the above vector
which(vector == 12)
  
# Getting the position of element 1
# in the above vector
which(vector == 1)
  
# Getting the position of element 6
# in the above vector
which(vector == 6)
  
# Getting the position of elements
# those are greater than 5
which(vector > 5)

输出:

[1] 5
[1] 3
[1] 4
[1] 4 5

示例 3:带有数据框的 which()函数

 在下面的示例中,which()函数用于查找具有数值的数据框中的列。

鸢尾花数据集用作数据框,其中包含 4 列数值和 1 列分类值,即物种。 which()函数从包含数值的数据集中查找列名称。

电阻



# Considering “Iris” dataset
data_set <- datasets::iris
  
# Printing the Iris dataset values
# along with its 5 columns out of which
# 4 columns are numerical and 1 is categorical
# (Species)
head(data_set)
  
# Calling the which() function over
# the above specified data set that 
# returns the columns with numeric values
Result <- which(sapply(data_set, is.numeric))
  
# Printing the columns with numeric values
colnames(data_set)[Result]

输出:

示例 4: which()函数与矩阵

在下面的示例中,which()函数用于查找指定矩阵中元素的位置。

这里正在计算指定矩阵中值 2 的位置。

电阻

# Creating a matrix of 3 columns and 4 rows
Matrix <- matrix(rep(c(1, 2, 3), 4), nrow = 4)
  
# Printing the entire matrix with its values
Matrix
  
# Calling the which() function to find the 
# position of value 2 in the above matrix
which(Matrix == 2, arr.ind = T)

输出: