首页 > 解决方案 > 一系列数字的比例 = >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

解决方案


问题在于序列。

尝试使用 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

问候!!


推荐阅读