简体   繁体   English

当其中一个观察满足特定条件时,如何删除组中的所有行?

[英]How do I drop all the rows within a group when one of the observations meets a certain condition?

I need to know how to drop all the rows prior to a certain point in a data.frame (DF1)我需要知道如何删除 data.frame (DF1) 中某个点之前的所有行

I want to drop all the rows before the row with the lowest number (grouped by ID and arranged by Date)我想删除编号最小的行之前的所有行(按 ID 分组并按日期排列)

DF1
ID     Date       Value
1      2/11/2021  0    
1      2/12/2021  2         
1      2/13/2021  1            
1      2/14/2021  0            
1      2/15/2021  1            
1      2/16/2021  -37            
1      2/17/2021  0           
1      2/18/2021  1            
1      2/19/2021  -2

The data frame I'm trying to get to (DF2):我试图访问的数据框(DF2):

DF2
ID     Date       Value       
1      2/16/2021  -37            
1      2/17/2021  0           
1      2/18/2021  1            
1      2/19/2021  -2

Thanks!谢谢!

You can use -您可以使用 -

library(dplyr)

df %>% 
  mutate(Date = as.Date(Date, '%m/%d/%Y')) %>%
  arrange(ID, Date) %>%
  group_by(ID) %>% 
  slice(which.min(Value):n()) %>%
  ungroup


#     ID Date       Value
#  <int> <date>     <int>
#1     1 2021-02-16   -37
#2     1 2021-02-17     0
#3     1 2021-02-18     1
#4     1 2021-02-19    -2

We may do我们可能会做

library(dplyr)
library(lubridate)
DF1 %>%
    arrange(ID, mdy(Date)) %>%
    group_by(ID) %>% 
    slice(match(min(Value), Value):n()) %>% 
    ungroup
# A tibble: 4 × 3
     ID Date      Value
  <int> <chr>     <int>
1     1 2/16/2021   -37
2     1 2/17/2021     0
3     1 2/18/2021     1
4     1 2/19/2021    -2

data数据

DF1 <- structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Date = c("2/11/2021", 
"2/12/2021", "2/13/2021", "2/14/2021", "2/15/2021", "2/16/2021", 
"2/17/2021", "2/18/2021", "2/19/2021"), Value = c(0L, 2L, 1L, 
0L, 1L, -37L, 0L, 1L, -2L)), class = "data.frame", row.names = c(NA, 
-9L))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM