How to not filter a data frame after a threshold

@andresrcs and @dromano have already (a) requested that you post a reprex, (b) been kind enough to provide guidance for how you can easily do so and (c) taken efforts to assist you despite the fact that have not posted one. By making just a little more effort, you could have made it so much easier for others to assist you. Please bear this in mind in the future.

@andresrcs has already taken a stab at answering your question. The solution posted, however, will only return observations for stocks that exceeded the threshold value after they have done so . Perhaps this is what you want, but from the manner in which you initially phrased your question, it seems that what you actually want is to:

  1. Identify all stocks that have ever exceeded the threshold value,
  2. Retain all of the observations for such stocks, and
  3. Discard all of the observations for all other stocks.

If this is in deed the case, the following should give you what you need (i.e. it will return all values for stocks y and z and remove all values for stock x). Note that I have used the same sample data used by @andresrcs:

# load library
library(tidyverse)

# create some mock data (similar output would have been produced if you used
# dput() on your data)
sample_df <- structure(
  list(
    t = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5),
    value = c(3, 4, 5, 6, 6, 5, 6, 7, 8, 9, 6, 7, 8, 5, 4),
    Stock = c("x", "x", "x", "x", "x", "y", "y", "y", "y", "y", "z", "z", "z", "z", "z")), 
  class = "data.frame", 
  row.names = c(NA, -15L)
)

# take sample_df %>% group by each stock identifier %>% retain each group's
# observations iff any of the values in the series have ever exceeded 7 %>%
# ungroup
sample_df %>% 
  group_by(Stock) %>% 
  filter(any(value > 7)) %>% 
  ungroup()
#> # A tibble: 10 x 3
#>        t value Stock
#>    <dbl> <dbl> <chr>
#>  1     1     5 y    
#>  2     2     6 y    
#>  3     3     7 y    
#>  4     4     8 y    
#>  5     5     9 y    
#>  6     1     6 z    
#>  7     2     7 z    
#>  8     3     8 z    
#>  9     4     5 z    
#> 10     5     4 z

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.