2014-09-18 56 views
-2

我有一个这样的表。我想选择那些对于每个不同名称coorrr并不总是大于budgetbudget总是大于coorrr的行。R或Python基于变量删除行

所以我想,例如只需TY作为budget有时大于coorrrcoorrr有时比budget更大的每个名称:

cluster load_date budget coorrr   name 
1  A 2/1/2014 12000 10000   Y 
2  A 3/1/2014 36000 290000   Y 
3  B 4/1/2014 15000 10000   N 
4  B 4/1/2014 12000 11500   N 
5  B 4/1/2014 90000 11000   T 
6  C 7/1/2014 22000 28000   T 
7  C 8/1/2014 30000 28960   T 
8  C 9/1/2014 53000 51200   T 

的代码,我到目前为止有:

import sys 
du=[] 
with open(sys.argv[1], 'r') as f1: 
    for line in f1: 
     pp = line.split("\t") 
    du.append(line.split("\t")[9]) 
    for aa in du: 
     if aa in line and pp[8] > pp[6] or pp[8] < pp[6]: 
      pass 
     else: 
      print line 

我会很高兴,如果你可以帮助我与R或Python或Bash,

+2

不清楚。也许你应该添加预期的结果。 – agstudy 2014-09-18 17:30:35

+0

你能更具体地描述你的问题吗?我明白你想要做什么,但是你发布的代码是做什么的?你可以与我们分享你的例外或不良结果吗?正如agstudy所说,向我们展示一个你想要的输出的例子将不仅仅是一句话的描述。 – skrrgwasme 2014-09-18 17:30:49

+0

我有点困惑,你想要'预算'和'coorr'不同的行吗? '如果aa符合并且pp [8]> pp [6]或pp [8] b10n 2014-09-18 17:32:48

回答

1

尝试(如果dat是数据)

dat[with(dat, !ave(budget>coorrr, name, FUN=all)),] 
#  cluster load_date budget coorrr name 
#1  A 2/1/2014 12000 10000 Y 
#2  A 3/1/2014 36000 290000 Y 
#5  B 4/1/2014 90000 11000 T 
#6  C 7/1/2014 22000 28000 T 
#7  C 8/1/2014 30000 28960 T 
#8  C 9/1/2014 53000 51200 T 

在这里,这个想法是由组列name使用ave功能,看看各name内所有元素都为TRUE的逻辑条件budget >coorr。与all

with(dat, budget >coorrr) 
    #[1] TRUE FALSE TRUE TRUE TRUE FALSE TRUE TRUE #Here, the 1st two elements belong to name `Y`, which are not all `TRUE` or all `FALSE`, while 3 and 4 elements from the comparison that belong to `N` are all `TRUE` 
    with(dat, budget <coorrr) #Here it got reversed 
#[1] FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE 

通过组合使用avefirst情况

with(dat, ave(budget>coorrr, name, FUN=all)) 
    #[1] FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE 

如果组中的所有元素是:如果你改变的条件budget <coorr

例如这应该工作TRUE,它将保持为TRUE,否则,整个组的索引更改为FALSE。因为你只想子集,它有一定的差异的行,只是用!

with(dat, !ave(budget>coorrr, name, FUN=all)) 
    #[1] TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE 

否定上述逻辑索引和子集你问它有什么[