根据作为另一数据帧的行给出的条件有效地过滤数据帧

时间:2018-11-04 08:27:16

标签: r filter dplyr

我有以下示例数据框:

<h1 class="title">New Contract</h1>

<form class="grid-wrapper" #f="ngForm">
  <div *ngIf="edit" class="form-group id">
    <label for="id">Transaction ID</label>
    <input id="id" type="text" name="id" class="form-control" disabled [(ngModel)]="contract.id">
  </div>

  <div class="form-group name">
    <label for="name">Contract name</label>
    <input id="name" type="text" name="name" class="form-control" required [(ngModel)]="contract.name">
  </div>

  <div class="form-group type">
    <div class="input-group-prepend">
      <label for="type">Merchant</label>
    </div>
    <select class="custom-select" name="type" [(ngModel)]="contract.merchant_id" id="merchant_id" required>
      <option selected>Please Select...</option>
      <option [value]="merchant.id" *ngFor="let merchant of merchants">{{ merchant.name }}</option>
    </select>
  </div>

  <div class="btn-row">
    <button class="btn btn-primary" [disabled]="f.invalid" (click)="submit()">Submit</button>
    <button class="btn btn-secondary" (click)="clear()">Clear</button>
  </div>

</form>

我想根据与df <- structure(list(PC1 = c(1.08553700088979, 3.0948497436612, -0.997456334603069, 1.41407630966724, 0.287288941434462, -0.304145457046063, 0.0540331738096902, 0.276994168448363, -0.178887591197422, 1.03793040779083, -0.964485366085487, 0.781189811085296, -0.360466840689429, -2.25639643892807, -0.688600791894463, 1.05031184739218, 3.30341296998208, 0.265388275042453, 0.187534314978584, 2.58042550274586, 0.564788667016578), PC2 = c(-0.560967999647005, 0.856204454728214, 0.720760276550347, 1.75595629874967, -0.707834522512927, 0.891530126176209, 0.631768747109977, -0.845237959897621, -0.412613566320007, -0.159362864836617, -0.569253016944671, -0.0181844049717689, -0.0218393445421908, 1.86197538876216, -0.263011388351398, 0.0582985416071711, 1.7585346351499, 1.74997701136744, 0.723398654405442, -0.482322211724498, -0.240535930597667), PC3 = c(0.36287528575844, -2.01764685704277, -0.408829080806452, 0.97914722241214, -0.665892667247256, -0.242401102421392, 0.497651711177106, 1.26726883331746, 1.27889899812577, 0.54485872382572, 0.191895005811088, 0.381351220912963, -0.613213748902156, 0.0685178101199476, 0.532000414181072, 1.19230092657081, 1.48731243525717, 1.16110479193897, 0.486880645956999, -2.69479147849705, 0.169949194117217)), row.names = c(NA, -21L), class = c("tbl_df", "tbl", "data.frame")) 相关的以下条件集过滤df的行,这些条件作为另一个数据帧PC1的行给出:

f1

PC2的过滤应根据f1 <- structure(list(xmin = c(-3.59811981997059, -3.10182743100913, -2.8536812365284, 2.8536812365284, 3.59811981997058), xmax = c(-3.34997362548985, -2.8536812365284, -2.60553504204766, 3.10182743100912, 3.84626601445132 )), row.names = c(NA, -5L), class = c("tbl_df", "tbl", "data.frame" )) 进行

f2

换句话说,数据帧f2 <- structure(list(xmin = c(-2.56910324629848, -2.37879930212822, 2.56910324629848, 2.949711134639), xmax = c(-2.37879930212822, -2.18849535795797, 2.75940719046874, 3.14001507880926)), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame")) 的列PC1中的值必须介于-3.6和-3.35之间,或介于-3.1和-2.85之间,依此类推,并且df的值必须介于-2.57和-2.38之间,依此类推。对于PC2的每一列,我都有一个数据框,它告诉我如何过滤相应的列。

我当然可以写出条件:

df

,然后对每一列重复此操作。但是最终我将有很多条件,这是不实际的。

是否有更短,更有效的方法?

谢谢!

1 个答案:

答案 0 :(得分:3)

使此功能生效的一种方法是使用glueevalparse函数。

我创建了一个函数(my_conditions),因此可以更轻松地使用它。更改列名称/条件表仍需要进行一些手动操作,但操作不多,而且可能也可以自动化。该函数调用glue包。

my_conditions <- function(column_name, condition_table){
  # create conditions
  conditions <- glue::glue("{column_name} > {condition_table$xmin} & {column_name} < {condition_table$xmax}")
  # collapse into 1 statement using " | " for or statement
  conditions <- paste0(conditions, collapse = " | ")
  return(conditions)
}

调用my_conditions("PC1", f1)的结果是一个长字符串,具有表f1的所有条件。

[1] "PC1 > -3.59811981997059 & PC1 < -3.34997362548985 | PC1 > -3.10182743100913 & PC1 < -2.8536812365284 | PC1 > -2.8536812365284 & PC1 < -2.60553504204766 | PC1 > 2.8536812365284 & PC1 < 3.10182743100912 | PC1 > 3.59811981997058 & PC1 < 3.84626601445132"

使用evalparse解析和评估代码中的条件。

使用dplyr:

df %>% 
  filter(eval(parse(text = my_conditions("PC1", f1))))
# A tibble: 1 x 3
    PC1   PC2   PC3
  <dbl> <dbl> <dbl>
1  3.09 0.856 -2.02

在基础R中进行过滤:只需将表名添加到列的前面

df[eval(parse(text = my_conditions("df$PC1", f1))), ]

# A tibble: 1 x 3
    PC1   PC2   PC3
  <dbl> <dbl> <dbl>
1  3.09 0.856 -2.02
相关问题