My data looks like this:
library(tidyverse)
set.seed(1)
df <- tibble(
id = c("cat", "cat", "mouse", "dog", "fish", "fish", "fish"),
value = rnorm(7, 100, sd = 50)
)
How might I "pop out" the top value of fish
, as in move fish to a new data frame and simultaneously remove it from the current data frame?
This works (but it doesn't seem all that elegant):
df_store <- df %>%
filter(id == "fish") %>%
top_n(1)
df <- anti_join(df, df_store)
Is there a better way?