Filter tibble in R when column names (to be filtered) and values are in vectors?

huangapple go评论95阅读模式
英文:

Filter tibble in R when column names (to be filtered) and values are in vectors?

问题

这可能是一个晦涩的问题或用例,但是否有一种快速的方法可以在列名和值都在向量内的情况下筛选一个tibble?

比方说,我想在mtcars中筛选mpghp。我可以这样做:

  1. filter(mtcars, mpg >= 15 & hp >= 100)

但相反,假设我有几个筛选案例,其中要筛选的列在一个向量中,而值在另一个向量中。在实际应用中,我可能在更大的数据框中有四到五个这样的案例。

  1. car_stat <- c('mpg', 'hp')
  2. car_value <- c(15, 100)

显然,这样不起作用。

  1. filter(mtcars, car_stat >= car_value)

但是否有一种简洁的dplyr/tidyverse方式可以使用向量进行筛选,或者我必须使用循环将其拆分为长度为1的单独向量?

英文:

This might be an esoteric question or use-case, but is there a quick way to filter a tibble when the column names and values are inside vectors?

Say I want to filter mpg and hp in mtcars. I could do something like:

  1. filter(mtcars, mpg &gt;= 15 &amp; hp &gt;= 100)

But instead, say I have several filtering cases -- with the columns to be filtered in one vector and the values in another. (In practice, I might have four or five cases in a larger df.)

  1. car_stat &lt;- c(&#39;mpg&#39;, &#39;hp&#39;)
  2. car_value &lt;- c(15, 100)

Obviously this doesn't work.

  1. filter(mtcars, car_stat &gt;= car_value)

But is there some succinct dplyr/tidyverse way to filter with vectors, or am I resigned to using some loop to break it up into separate vectors, each of length one?

答案1

得分: 5

使用您的变量和数值,您可以将它们转化为过滤表达式。在这里,我们使用基本的R Mapbquote 函数。

  1. car_stat <- c('mpg', 'hp')
  2. car_value <- c(15, 100)
  3. criteria <- unname(Map(function(c, v) bquote(.(as.name(c)) >= .(v)), car_stat, car_value))
  4. criteria
  5. # [[1]]
  6. # mpg >= 15
  7. #
  8. # [[2]]
  9. # hp >= 100

这将创建一个表达式的列表,用于过滤。然后,您可以使用 !!! 将它们传递给 filter 函数。

  1. dplyr::filter(mtcars, !!!criteria)
  2. # mpg cyl disp hp drat wt qsec vs am gear carb
  3. # Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
  4. # Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
  5. # Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
  6. # Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
  7. # ...

以上是您要求的翻译。

英文:

Using your variables and values, you can turn those into filtering expressions. Here we use the base R Map and bquote functions

  1. car_stat &lt;- c(&#39;mpg&#39;, &#39;hp&#39;)
  2. car_value &lt;- c(15, 100)
  3. criteria &lt;- unname(Map(function(c, v) bquote(.(as.name(c))&gt;=.(v)), car_stat, car_value))
  4. criteria
  5. # [[1]]
  6. # mpg &gt;= 15
  7. #
  8. # [[2]]
  9. # hp &gt;= 100

This creates a list of expressions that you want for your filter. Then you can inejct them to filter with !!!

  1. dplyr::filter(mtcars, !!!criteria)
  2. # mpg cyl disp hp drat wt qsec vs am gear carb
  3. # Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
  4. # Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
  5. # Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
  6. # Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
  7. # ...

答案2

得分: 1

以下是翻译的代码部分:

  1. 这里是另一种方法,利用了data.table 1.14.9`env`参数。
  1. library(data.table)
  2. cars = setDT(copy(mtcars))
  3. do.call(
  4. fintersect,
  5. lapply(1:2, \(i) cars[k>=z, env = list(k=car_stat[i], z =car_value[i])])
  6. )
  1. 输出:
  1. mpg cyl disp hp drat wt qsec vs am gear carb id
  2. 1: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 1
  3. 2: 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 2
  4. 3: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 4
  5. 4: 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 5
  6. 5: 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 6
  7. 6: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 10
  8. 7: 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 11
  9. 8: 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 12
  10. 9: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 13
  11. 10: 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 14
  12. 11: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 22
  13. 12: 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 23
  14. 13: 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 25
  15. 14: 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 28
  16. 15: 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 29
  17. 16: 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 30
  18. 17: 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 31
  19. 18: 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 32
英文:

Here is another approach that leverages the env parameter of data.table 1.14.9

  1. library(data.table)
  2. cars = setDT(copy(mtcars))
  3. do.call(
  4. fintersect,
  5. lapply(1:2, \(i) cars[k&gt;=z, env = list(k=car_stat[i], z =car_value[i])])
  6. )

Output:

  1. mpg cyl disp hp drat wt qsec vs am gear carb id
  2. 1: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 1
  3. 2: 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 2
  4. 3: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 4
  5. 4: 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 5
  6. 5: 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 6
  7. 6: 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 10
  8. 7: 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 11
  9. 8: 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 12
  10. 9: 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 13
  11. 10: 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 14
  12. 11: 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 22
  13. 12: 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 23
  14. 13: 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 25
  15. 14: 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 28
  16. 15: 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 29
  17. 16: 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 30
  18. 17: 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 31
  19. 18: 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 32

huangapple
  • 本文由 发表于 2023年3月4日 06:31:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定