1.函数功能
Give the TRUE indices of a logical object, allowing for array indices.
2. 函数语法
which(x, arr.ind = FALSE, useNames = TRUE)
3. 函数参数
3.1 X
x
a logical vector or array. NAs are allowed and omitted (treated as if FALSE).
逻辑型向量或者数组。允许出现空值,视为FALSE
3.2 arr.ind
arr.ind
logical; should array indices be returned when x is an array?
3.3 ind
ind
integer-valued index vector, as resulting from which(x).
3.4 useNames
useNames
logical indicating if the value of arrayInd() should have (non-null) dimnames at all.
不太理解每个参数的含义
具体使用情况如下:
返回值为:满足条件的下标
# which函数
# 1. 返回满足条件的向量下标
cy <- c(1,4,3,7,NA)
cy
which(cy>3) #返回下标
cy[which(cy>3)] #返回下标对应的值
# 2.数组中使用
ay <- array(rep(c(4,6,2,1,3,7),2),dim=c(2,3,2))
ay
which(ay>6)
which(ay>6,arr.ind=TRUE)
ay[which(ay>6,arr.ind=TRUE)]
which(ay>6,arr.ind=TRUE,useNames=F)
# 3. 数据框中使用
studentID <- c(1,2,3,4,5,6)
gender <- c('M','F','M','M','F','F')
math <- c(40,60,70,60,90,60)
English <- c(98,56,78,93,79,78)
Chinese <- c(86,54,78,90,78,54)
data <- data.frame(studentID,gender,math,English,Chinese,stringsAsFactors = F)
data
which(data[,5]==78)
#挑选语文成绩为78的学生
data[which(data[,5]==78),]