r - if_else 用于删除每组中的第一行 - dplyr

标签 r dplyr tidyverse

她是一个用于测试的 DF:

test_df <- structure(list(plant_id = c("plant_1", "plant_1", "plant_1", "plant_1", "plant_1",
                                       "plant_2", "plant_2", "plant_2", "plant_2", "plant_2", 
                                       "plant_3", "plant_3", "plant_3", "plant_3", "plant_3",
                                       "plant_4", "plant_4", "plant_4", "plant_4", "plant_4"), 
                          skipped = c(1, 1, 0, 1, 2, 
                                      0, 1, 1, 0, 2,
                                      1, 0, 1, 2, 2, 
                                      0, 0, 1, 1, 2)), 
                     row.names = c(NA, -20L), class = "data.frame", 
                     .Names = c("plant_sp", "skipped"))

如您所见,变量skipped的值为“0”、“1”或“2”。 我需要每个 plant_id (这是 group_by 变量), 当跳过列的第一行为“1”时,该行将被删除,直到跳过列发生更改。

例如在我的 DF 中:

   plant_sp skipped
1   plant_1       1
2   plant_1       1
3   plant_1       0
4   plant_1       1
5   plant_1       2
6   plant_2       0
7   plant_2       1
8   plant_2       1
9   plant_2       0
10  plant_2       2
11  plant_3       1
12  plant_3       0
13  plant_3       1
14  plant_3       2
15  plant_3       2
16  plant_4       0
17  plant_4       0
18  plant_4       1
19  plant_4       1
20  plant_4       2

致:

   plant_sp skipped
   plant_sp skipped

3   plant_1       0
4   plant_1       1
5   plant_1       2
6   plant_2       0
7   plant_2       1
8   plant_2       1
9   plant_2       0
10  plant_2       2
12  plant_3       0
13  plant_3       1
14  plant_3       2
15  plant_3       2
16  plant_4       0
17  plant_4       0
18  plant_4       1
19  plant_4       1
20  plant_4       2

如您所见,由于组“planet_1”和组“planet_2”以“1”开头,因此变量开头为“1”的所有行跳过已删除(第 1 行和第 2 行)。所有其他行保持原样。

如果可能的话,dplyr 解决方案会很棒, 非常感谢!!!

最佳答案

更新:原始版本不满足查询,现在已更新为仅删除非 1 条目之前包含 1 的行。这与查询中显示的输出相匹配。

为了尝试完成此操作,我创建了一些临时行来标识每个组中不包含行的第一行,然后删除在此之前的所有行

library(tidyverse)

test_df <- structure(list(plant_id = c("plant_1", "plant_1", "plant_1", "plant_1", "plant_1",
                                       "plant_2", "plant_2", "plant_2", "plant_2", "plant_2", 
                                       "plant_3", "plant_3", "plant_3", "plant_3", "plant_3",
                                       "plant_4", "plant_4", "plant_4", "plant_4", "plant_4"), 
                          skipped = c(1, 1, 0, 1, 2, 
                                      0, 1, 1, 0, 2,
                                      1, 0, 1, 2, 2, 
                                      0, 0, 1, 1, 2)), 
                     row.names = c(NA, -20L), class = "data.frame", 
                     .Names = c("plant_sp", "skipped"))

test_df <- tibble(test_df)

first_positions_df<- test_df %>% 
  # group by each factor we want
  group_by(plant_sp) %>%
  # label the order of the rows
  mutate(order = 1:length(skipped)) %>% 
  # mark position of rows which aren't a 1 otherwise set to infinity
  mutate(notones = ifelse(skipped != 1, order, Inf)) %>% 
  # Find the first position which is not a 1
  mutate(ignore = min(notones)) %>% 
  # Remove all ones before this row
  filter(ignore <= order)

#Final result
first_positions_df %>% 
  # select only the useful columns
  select(plant_sp, skipped)
#> # A tibble: 17 x 2
#> # Groups:   plant_sp [4]
#>    plant_sp skipped
#>    <chr>      <dbl>
#>  1 plant_1        0
#>  2 plant_1        1
#>  3 plant_1        2
#>  4 plant_2        0
#>  5 plant_2        1
#>  6 plant_2        1
#>  7 plant_2        0
#>  8 plant_2        2
#>  9 plant_3        0
#> 10 plant_3        1
#> 11 plant_3        2
#> 12 plant_3        2
#> 13 plant_4        0
#> 14 plant_4        0
#> 15 plant_4        1
#> 16 plant_4        1
#> 17 plant_4        2

reprex package 于 2021 年 4 月 4 日创建(v2.0.0)

关于r - if_else 用于删除每组中的第一行 - dplyr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66942594/

相关文章:

r - 分层/嵌套引导方法

R data.table - 如何在多列的固定行上插入缺失值

r - R 中支持 Callback_Early_Stopping

r - 在表格输出上方添加几行文本

r - 是否可以根据变量标签选择列?

有效地 reshape 总结的结果

r - 相当于 r 中的 let 表达式

按组排序变量 (dplyr)

r - 连接多个数据框时使用数据框名称作为后缀

r - combn 上的 Tidyverse 友好 hack