英文:
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 <- 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
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 |>
filter(if_any(b:c, ~ a >= .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 |>
filter(if_any(b:c, ~ a >= .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 <- 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
With updated dataset:
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
<sup>Created on 2023-06-19 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论