gpt4 book ai didi

r - 如果一个选择具有给定值,则选择整个组

转载 作者:行者123 更新时间:2023-12-01 03:32:16 27 4
gpt4 key购买 nike

 datetime                                label            option_title option_value  lead difference

1 2016-07-22 GE 3 - Commercial Review 3 2 -1
2 2017-02-20 GE 2 - Solution Review 2 1 -1
3 2017-02-20 GE 1 - Opportunity Review 1 2 1
4 2017-04-18 GE 2 - Solution Review 2 3 1
5 2017-04-19 GE 3 - Commercial Review 3 4 1
6 2017-04-19 GE 4 - Submit Proposal 4 5 1
7 2017-08-08 GE 5 - Proposal Awarded 5 NA NA
8 2016-08-02 HSBC 5 - Proposal Awarded 5 6 1
9 2016-12-13 HSBC 6 - Delivery Phase 1 6 7 1
10 2017-08-07 HSBC 7 - Phase 1 Live 7 NA NA
11 2016-07-22 Lowes Pre-Qualification 0 NA NA
12 2016-08-02 Danske Bank 6 - Delivery Phase 1 6 NA NA
13 2016-07-22 AP Moller Maersk (IT Transformation) 3 - Commercial Review 3 NA NA
14 2016-07-22 BHP Billiton - APJ Pre-Qualification 0 2 2
15 2016-07-26 BHP Billiton - APJ 2 - Solution Review 2 0 -2
16 2016-07-26 BHP Billiton - APJ Pre-Qualification 0 2 2

我想从这个数据框创建一个新的数据框,它只选择具有负“差异”值的“标签”。但是,我想像这样选择所有类似的“标签”:

 datetime                                label            option_title option_value  lead difference

1 2016-07-22 GE 3 - Commercial Review 3 2 -1
2 2017-02-20 GE 2 - Solution Review 2 1 -1
3 2017-02-20 GE 1 - Opportunity Review 1 2 1
4 2017-04-18 GE 2 - Solution Review 2 3 1
5 2017-04-19 GE 3 - Commercial Review 3 4 1
6 2017-04-19 GE 4 - Submit Proposal 4 5 1
7 2017-08-08 GE 5 - Proposal Awarded 5 NA NA
8 2016-07-22 BHP Billiton - APJ Pre-Qualification 0 2 2
9 2016-07-26 BHP Billiton - APJ 2 - Solution Review 2 0 -2
10 2016-07-26 BHP Billiton - APJ Pre-Qualification 0 2 2

我不确定如何在 dplyr 中执行此操作……SQL 会更好吗? (我没怎么用过R中的sql包)

最佳答案

另一种可能的方法是使用 dplyr:

library(dplyr)

df %>% group_by(label) %>% filter(any(difference < 0))

#> # A tibble: 10 x 6
#> # Groups: label [2]
#> datetime label option_title option_value lead
#> <date> <chr> <chr> <int> <int>
#> 1 2016-07-22 GE 3 - Commercial Review 3 2
#> 2 2017-02-20 GE 2 - Solution Review 2 1
#> 3 2017-02-20 GE 1 - Opportunity Review 1 2
#> 4 2017-04-18 GE 2 - Solution Review 2 3
#> 5 2017-04-19 GE 3 - Commercial Review 3 4
#> 6 2017-04-19 GE 4 - Submit Proposal 4 5
#> 7 2017-08-08 GE 5 - Proposal Awarded 5 NA
#> 8 2016-07-22 BHP Billiton - APJ Pre-Qualification 0 2
#> 9 2016-07-26 BHP Billiton - APJ 2 - Solution Review 2 0
#> 10 2016-07-26 BHP Billiton - APJ Pre-Qualification 0 2
#> # ... with 1 more variables: difference <int>

数据

library(readr)
df <- read_csv("rowid, datetime, label, option_title, option_value, lead, difference
1, 2016-07-22, GE, 3 - Commercial Review, 3, 2, -1
2, 2017-02-20, GE, 2 - Solution Review, 2, 1, -1
3, 2017-02-20, GE, 1 - Opportunity Review, 1, 2, 1
4, 2017-04-18, GE, 2 - Solution Review, 2, 3, 1
5, 2017-04-19, GE, 3 - Commercial Review, 3, 4, 1
6, 2017-04-19, GE, 4 - Submit Proposal, 4, 5, 1
7, 2017-08-08, GE, 5 - Proposal Awarded, 5, NA, NA
8, 2016-08-02, HSBC, 5 - Proposal Awarded, 5, 6, 1
9, 2016-12-13, HSBC, 6 - Delivery Phase 1, 6, 7, 1
10, 2017-08-07, HSBC, 7 - Phase 1 Live, 7, NA, NA
11, 2016-07-22, Lowes, Pre-Qualification, 0, NA, NA
12, 2016-08-02, Danske Bank, 6 - Delivery Phase 1, 6, NA, NA
13, 2016-07-22, AP Moller Maersk (IT Transformation), 3 - Commercial Review, 3, NA, NA
14, 2016-07-22, BHP Billiton - APJ, Pre-Qualification, 0, 2, 2
15, 2016-07-26, BHP Billiton - APJ, 2 - Solution Review, 2, 0, -2
16, 2016-07-26, BHP Billiton - APJ, Pre-Qualification, 0, 2, 2")

df <- df[-1]

关于r - 如果一个选择具有给定值,则选择整个组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47282207/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com