计算通过和失败每种条件的观察值 [英] Count observations that pass and fail each condition

查看:62
本文介绍了计算通过和失败每种条件的观察值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对上一个问题的后续操作,该问题显示在此处

This is a follow-up to a previous question shown here.

我有类似以下数据:

set.seed(1)
dt <- data.table(ID=1:10, Status=c(rep("OUT",2),rep("IN",2),"ON",rep("OUT",2),rep("IN",2),"ON"), 
                 t1=round(rnorm(10),1), t2=round(rnorm(10),1), t3=round(rnorm(10),1), 
                 t4=round(rnorm(10),1), t5=round(rnorm(10),1), t6=round(rnorm(10),1),
                 t7=round(rnorm(10),1),t8=round(rnorm(10),1))

    ID Status   t1   t2   t3   t4   t5   t6   t7   t8
 1:  1    OUT -0.6  1.5  0.9  1.4 -0.2  0.4  2.4  0.5
 2:  2    OUT  0.2  0.4  0.8 -0.1 -0.3 -0.6  0.0 -0.7
 3:  3     IN -0.8 -0.6  0.1  0.4  0.7  0.3  0.7  0.6
 4:  4     IN  1.6 -2.2 -2.0 -0.1  0.6 -1.1  0.0 -0.9
 5:  5     ON  0.3  1.1  0.6 -1.4 -0.7  1.4 -0.7 -1.3
 6:  6    OUT -0.8  0.0 -0.1 -0.4 -0.7  2.0  0.2  0.3
 7:  7    OUT  0.5  0.0 -0.2 -0.4  0.4 -0.4 -1.8 -0.4
 8:  8     IN  0.7  0.9 -1.5 -0.1  0.8 -1.0  1.5  0.0
 9:  9     IN  0.6  0.8 -0.5  1.1 -0.1  0.6  0.2  0.1
10: 10     ON -0.3  0.6  0.4  0.8  0.9 -0.1  2.2 -0.6

我将约束应用于从csv读取的 dt

I apply constraints to dt that are read in from a csv:

dt_constraints <- data.table(columns=c("t1","t3","t7","t8"), operator=c(rep(">=",2),rep("<=",2)), 
                             values=c(-.6,-.5,2.4,.5))

    columns operator    values
1   t1       >=         -0.6
2   t3       >=         -0.5
3   t7       <=          2.4
4   t8       <=          0.5

@akrun提供了一个非常有效的解决方案:

@akrun provided a very efficient solution:

dt[eval(parse(text=do.call(paste, c(dt_constraints, collapse= ' & '))))]

我还需要另外两个信息:通过每个观察的数量约束以及未通过每个约束的观察数。输出可能如下所示:

I need two additional bits of information: the number of observations that pass each constraint and the number of observations that fail each constraint. The output could look like this:

    columns operator    values  obs_pass  obs_fail
1   t1       >=         -0.6       8         2
2   t3       >=         -0.5       8         2
3   t7       <=          2.4       10        0
4   t8       <=          0.5       9         1

任何想法都将不胜感激。谢谢。

Any ideas will be greatly appreciated. Thanks.

推荐答案

您可以构造 on = 表达式并执行加入每一行:

You could construct the on= expression and do a join for each row:

dt_constraints[, onexpr := sprintf("%s%sv", columns, operator)]

dt_constraints[, n_pass := dt[.(v = values), on=onexpr, .N, by=.EACHI]$N, by=onexpr]
dt_constraints[, n_fail := nrow(dt) - n_pass ]

by = .EACHI 应该允许您将相同的 onexpr 与多个不同的一起使用,尽管我猜有更好的选择做到这一点的方法。如果 onexpr 是唯一的,那么...

The by=.EACHI should allow you to use the same onexpr with multiple different values, though I guess there are better ways to do that. If onexpr are unique, there's instead...

dt_constraints[, n_pass := dt[.(v = values), on=onexpr, .N], by=onexpr]
dt_constraints[, n_fail := nrow(dt) - n_pass ]






使用此方法一次过滤所有约束:*


To filter on all constraints at once using this approach:*

n_grp = nrow(dt_constraints)
w = seq_len(nrow(dt))
dt[dt_constraints[, {
  w <- w[dt[w][.(v = values), on=onexpr, which=TRUE]]
  if (.GRP == n_grp) w
}, by=onexpr]$V1]

   ID Status   t1  t2   t3   t4   t5   t6   t7   t8
1:  1    OUT -0.6 1.5  0.9  1.4 -0.2  0.4  2.4  0.5
2:  2    OUT  0.2 0.4  0.8 -0.1 -0.3 -0.6  0.0 -0.7
3:  5     ON  0.3 1.1  0.6 -1.4 -0.7  1.4 -0.7 -1.3
4:  7    OUT  0.5 0.0 -0.2 -0.4  0.4 -0.4 -1.8 -0.4
5:  9     IN  0.6 0.8 -0.5  1.1 -0.1  0.6  0.2  0.1
6: 10     ON -0.3 0.6  0.4  0.8  0.9 -0.1  2.2 -0.6

这可以迭代l像一个循环,只检查那些先前具有约束的行( w )。

This works iteratively like a loop, only checking those rows (w) for which prior constraints held.

*假定没有重复的 onexpr ,因为那没有道理..您可以先将每个 onexpr 过滤为最大 > = 的值< = 的最小值。

*assuming no repeated onexpr, since that wouldn't make sense.. you could first filter per onexpr to max values for >= and min for <=.

OP的后续问题:


我将如何更改代码以执行以下操作:(1)给出第一个约束的通过/失败计数; (2)使用来自第一约束的通过子集的数据,给出第二约束的通过/失败计数; (3)使用第二个约束的通过次数子集的数据,给出第三个约束的通过/失败计数,依此类推。

How would I alter the code to perform the following: (1)give pass/fail counts for the first constraint; (2)using data subsetted by passes from the first constraint, give the pass/fail counts for the second constraint; (3)using data subsetted by passes from the second constraint, give the pass/fail counts for the third constraint, etc.



# iteratively count pass/fail
w = seq_len(nrow(dt))
dt_constraints[, {
  w0 <- w
  w <- w[dt[w][.(v = values), on=onexpr, which=TRUE]]
  .(n_pass = length(w), n_fail = length(w0) - length(w))
}, by=onexpr]

这篇关于计算通过和失败每种条件的观察值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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