删除行,如果第1列的值低于第2列和第3列的值?

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

How to remove rows if column 1 value is lower than column 2 and column 3?

问题

我想知道如何在列1的值低于列2和列3时删除行。

以下是虚拟数据集的更新:

c <- c(10, 20, 30, 40, 50)
b <- c(40, 2, 40, 10, 50)
a <- c(10, 50, 0.0000891, 60, 100)
id <- c("a", "b", "c", "d", "e")

data <- data.frame(id, a, b, c)
head(data)
#  id        a  b  c
#1  a       10 40 10
#2  b       50  2 20
#3  c   8.91e-05 1 0
#4  d       60 10 40
#5  e      100 50 50

在上述虚拟数据集中,我们可以看到例如:data$id "c",列a的值低于列b和列c。

在条件删除后,以下是预期输出:

#  id   a  b  c
#1  b  50  2 20
#2  d  60 10 40
#3  e 100 50 50

如果可能的话,更倾向于使用tidyverse方法。谢谢。

英文:

I would like to know how to remove rows if column 1 value is lower than column 2 and column 3 ?

Here is dummy datasets Updated:

c &lt;- c(10, 20, 30, 40, 50)
b &lt;- c(40, 2, 40, 10, 50)
a &lt;- c(10, 50, 0.0000891, 60, 100)
id &lt;- c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;)

data &lt;- data.frame(id, a, b, c)
head(data)
#  id        a  b  c
#1  a       10 40 10
#2  b       50  2 20
#3  c   8.91e-05 1 0
#4  d       60 10 40
#5  e      100 50 50

In the above dummy dataset we can see that for example: data$id "c", a column value is lower than "b" and "c" column.

After conditional removalhere is the expected output:

#  id   a  b  c
#1  b  50  2 20
#2  d  60 10 40
#3  e 100 50 50

If its possible tidyverse approach more preferred. Thank you.

答案1

得分: 3

另一种允许使用 if_any 进行动态选择的方式:

library(dplyr)
data |&gt; 
  filter(if_any(b:c, ~ a &gt;= .x))

#  id   a  b  c
#1  a  10 40 10
#2  b  50  2 20
#3  d  60 10 40
#4  e 100 50 50

(请注意,这里的代码部分没有进行翻译。)

英文:

Another way that allows dynamic selection with if_any:

library(dplyr)
data |&gt; 
  filter(if_any(b:c, ~ a &gt;= .x))

#  id   a  b  c
#1  a  10 40 10
#2  b  50  2 20
#3  d  60 10 40
#4  e 100 50 50

答案2

得分: 2

以下是代码部分的翻译:

library(dplyr)

# 原始数据集

c <- c(10, 20, 30, 40, 50)
b <- c(40, 2, 40, 10, 50)
a <- c(10, 50, 1, 60, 100)
id <- c("a", "b", "c", "d", "e")

data <- data.frame(id, a, b, c)

data %>%
  filter(a > b | a > c)
#> # A tibble: 4 × 4
#>   id        a     b     c
#>   <chr> <dbl> <dbl> <dbl>
#> 1 a        10    40    10
#> 2 b        50     2    20
#> 3 d        60    10    40
#> 4 e       100    50    50

更新后的数据集:


c <- c(10, 20, 30, 40, 50)
b <- c(40, 2, 40, 10, 50)
a <- c(10, 50, 0.0000891, 60, 100)
id <- c("a", "b", "c", "d", "e")

data <- data.frame(id, a, b, c)

data %>%
  filter(a > b | a > c)
#>   id   a  b  c
#> 1  b  50  2 20
#> 2  d  60 10 40
#> 3  e 100 50 50

创建于2023年06月19日,使用reprex v2.0.2

英文:

Does this work for you?
Updated to reflect @Mael's comment.

library(dplyr)

#original dataset

c &lt;- c(10, 20, 30, 40, 50)
b &lt;- c(40, 2, 40, 10, 50)
a &lt;- c(10, 50, 1, 60, 100)
id &lt;- c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;)

data &lt;- data.frame(id, a, b, c)

data |&gt; 
  filter(a &gt; b | a &gt; c)
#&gt; # A tibble: 4 &#215; 4
#&gt;   id        a     b     c
#&gt;   &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
#&gt; 1 a        10    40    10
#&gt; 2 b        50     2    20
#&gt; 3 d        60    10    40
#&gt; 4 e       100    50    50

With updated dataset:


c &lt;- c(10, 20, 30, 40, 50)
b &lt;- c(40, 2, 40, 10, 50)
a &lt;- c(10, 50, 0.0000891, 60, 100)
id &lt;- c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;)

data &lt;- data.frame(id, a, b, c)

data |&gt; 
  filter(a &gt; b | a &gt; c)
#&gt;   id   a  b  c
#&gt; 1  b  50  2 20
#&gt; 2  d  60 10 40
#&gt; 3  e 100 50 50

<sup>Created on 2023-06-19 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月19日 15:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504324.html
匿名

发表评论

匿名网友

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

确定