计算R范围内的向量值数量 [英] Count number of vector values in range with R

查看:110
本文介绍了计算R范围内的向量值数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,如果您在向量而不是标量上测试条件,它将返回一个向量,其中包含向量中每个值的比较结果。例如...

In R, if you test a condition on a vector instead of a scalar, it will return a vector containing the result of the comparison for each value in the vector. For example...

> v <- c(1,2,3,4,5)
> v > 2
[1] FALSE FALSE  TRUE  TRUE  TRUE

这样,我可以确定向量中大于或小于一定数量的元素的数量,例如。

In this way, I can determine the number of elements in a vector that are above or below a certain number, like so.

> sum(v > 2)
[1] 3
> sum(v < 2)
[1] 1

有人知道我可以确定给定范围内的值数量?例如,如何确定大于2但小于5的值的数量?

Does anyone know how I can determine the number of values in a given range? For example, how would I determine the number of values greater than 2 but less than 5?

推荐答案

尝试

> sum(v > 2 & v < 5)

这篇关于计算R范围内的向量值数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆