R语言 返回逻辑对象的真实指数 – which()函数

R语言 返回逻辑对象的真实指数 – which()函数

R语言中的 which() 函数用于返回作为参数传递的逻辑运算中返回真值的对象的索引。

语法: which(x, arr.ind)

参数:

x: 逻辑对象

arr.ind: 显示索引的布尔值

例1 :

# R program to illustrate 
# the use of which() function
  
# Create a matrix
x <- matrix(1:9, 3, 3)
x
  
# Calling which() function
which(x %% 2 == 0, arr.ind = TRUE)

输出

     [, 1] [, 2] [, 3]
[1, ]    1    4    7
[2, ]    2    5    8
[3, ]    3    6    9
     row col
[1, ]   2   1
[2, ]   1   2
[3, ]   3   2
[4, ]   2   3

在这里,在上述代码中,which()函数返回矩阵中所有偶数的索引。

例2 :

# R program to illustrate 
# the use of which() function
  
# Using predefined dataset
BOD
  
# Calling which() function
which(BOD$demand == 19, arr.ind = TRUE)

输出

  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
[1] 3

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程