Group_by ID, the keep row with attribute R

Viewed 22

Table:

ID <- c("01", "01", "02", "02) 
Accept_Medicare <- c("Opt-out", "Accept", "Opt-Out", "Accept")
Data <- c("yes", "no", "no", "no")

I have a dataset with multiple of the same ID, and a column "Accept_Medicare." I want to deduplicate the data set to only have one ID per row. Within each ID, I want to select the row that has the value "Opt-Out" in the "Accept_Medicare" column. Each ID only has one row where Accept_Medicare can be "Opt-Out". Here's what I've tried but it's not picking the "Opt-Out" rows.

combo1 <- combo1 %>% group_by(ID) %>% 
  summarise(Status = any(`Accept_Medicare` == "Opt-Out"), across(everything(), 
~first(.x))

I would like the result to be two rows with this data.

01 | "Opt-out" | "yes"
02 | "Opt-out" | "yes"

Thank you in advance for help!

1 Answers

we are looking for group_by followed by filter(Accept_Medicare == "Opt-Out")

library(dplyr)

combo1 <- combo1 %>%
          group_by(ID) %>%
          filter(Accept_Medicare == "Opt-Out")
Related