1

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!

benzinga
  • 49
  • 4
  • There's a missing quotation mark in the code that defines `ID` and the code to generate the data frame `combo1` is missing. Apart from that: I think you need to try dplyr `group_by` and `filter`. – neilfws Sep 14 '22 at 00:12

1 Answers1

0

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")
GuedesBF
  • 8,409
  • 5
  • 19
  • 37