which函数--R语言

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),]
  • 6
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值