r - 一系列数字的比例 = >1
问题描述
我正在尝试确定长数据帧上子集的一系列数字的比例。(目的是编写一个函数。)
below.green<-mean(results$Value <0.04)
green.amber<-mean(results$Value >0.04:0.4)
amber.red<-mean(results$Value >0.4:4)
red.plus<- mean(results$Value >4)
meanresults <- c(below.green,green.amber,amber.red,red.plus)
例如1
Values <- c(0.1501,0.1276,0.0838,0,0,0.4544,0.2573,0.1788,1.291,1.4737,1.8191,
0.5986,4.5846,4.9056,2.4809,2.1021,3.3741,0.0085,0.0302,0.0033,0.0405,0,0,0,0,0,
0.3262,0.0462,0.2536,0.3661,0.4311,0.4719,0.8482,2.3731,0.656,0.3967,0.0399,
0.0302,0.2723,0.3833,0.5907,0.3725,0.0258,0.0483)
sum(meanresults)
#[1] 1.247892
例2
Values2 <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.0726,0.0077,0.0444)
sum(meanresults)
#[1] 1
当我的比例在某些子集上返回时,比例等于 >1(参见例如 1)。其他站点的总比例 =1(例如 2)这似乎只发生在 Value <0.4 的站点上。我哪里错了?
我查看了网站上的多个 QA 并没有找到类似的示例。
解决方案
问题在于序列。
尝试使用 R 函数between
(data.table 包)编写代码。
一些例子:
> 0.04:0.4
[1] 0.04
> 0.3 < 0.04:0.4
[1] FALSE
> between(0.3, 0.04, 0.4)
[1] TRUE
> 0.4:4
[1] 0.4 1.4 2.4 3.4
> c(0.3, 0.5) > 0.4:4 #Only use the first value
[1] FALSE FALSE FALSE FALSE
> between(c(0.3, 0.5), 0.4, 4)
[1] FALSE TRUE
查看帮助 (?between) 和参数 incbounds 来设置间隔。
> between(c(0.3, 0.5), 0.3, 4)
[1] TRUE TRUE
> between(c(0.3, 0.5), 0.3, 4, incbounds = FALSE)
[1] FALSE TRUE
问候!!
推荐阅读
- azure - Azure DevOps 日历 - 不允许使用“添加休息日”
- java - 如何在异常堆栈中停止 log4j2 加载类?
- python - 如何在 Pyspark 中 Srot rdd 内部列表元素?
- ios - UITests-如何在 UITests 中获取当前语言
- python - 我们如何在 Windows 中将模型保存为 .pb 文件
- java - Foursquare 场地 API 返回空场地列表
- reactjs - Redux: mapStateToProps 为什么第二个参数ownProps
- perl - Perl:在操作员期望的位置找到裸字
- gmail-api - gmail.modify 用于 e2e 测试的范围 Oauth 验证
- sql - 需要oracle sql查询