I have to delete a row in a dataframe and get the index of the deleted row.
In practice I click on a row of a table in a shiny app and delete it with an action button:
From this row I need the index:
Here is an example to demonstrate what I would like to do:
library(dplyr)
df1 <- diamonds %>%
select(1:2) %>%
slice(1:5)
carat cut
<dbl> <ord>
1 0.23 Ideal
2 0.21 Premium
3 0.23 Good
4 0.29 Premium
5 0.31 Good
From df1 I delete row 3 and get df2. But I need the index of the deleted row as vector.
df2 <- df1 %>%
slice(-3)
carat cut
<dbl> <ord>
1 0.23 Ideal
2 0.21 Premium
3 0.29 Premium
4 0.31 Good
I have tried:
anti_join(df1, df2) %>%
rownames_to_column("id") %>%
pull(id)
which gives:
[1] "1"
Expected Output
[1] "3"