Select all rows based on conditions in two columns in R (multiple cases per row)

Viewed 125

Okay, I'm a beginner and have issues formulating my questions adequately, so please redirect me if I can find the answer somewhere else and forgive me :)

I'm working with a messy big data patient file (> 40 million rows). Each patient (id) has several rows. Each row (roughly) represents one consultation with a symptom/disease code (icpc). I want to reduce the dataset to all rows of patients with specific conditions (icpc) in one of their consultations/rows, but keep all rows with other conditions for these patients as well. I would also like to keep the column reg.date.

My raw data.frame (x) looks something like this (this is fabricated data, id is much longer in my dataset and I left out irrelevant columns which I like to drop):

id icpc reg.date
123 D95 19JUN2015
123 F85 15AUG2016
332 A01 16MAR2010
332 A04 20JAN2018
332 K20 20FEB2017
100 B10 01JUN2017
100 A04 11JAN2008
113 T08 18MAR2018
113 P28 19JAN2017
113 D95 16JAN2013
113 A01 01MAY2009
551 B12 03APR2011
551 D95 09MAY2015

Say I want to only to keep the patients with D95 and/or A01 in the dataset. I've managed subsetting:

subset(x, ICPC == c("D95", "A01"))

But this leaves me only with the consultations where D95 and A01 was registered:

id icpc reg.date
123 D95 19JUN2015
332 A01 16MAR2010
113 D95 16JAN2013
113 A01 01MAY2009
551 D95 09MAY2015

This is the data.frame I want:

id icpc reg.date
123 D95 19JUN2015
123 F85 15AUG2016
332 A01 16MAR2010
332 A04 20JAN2018
332 K20 20FEB2017
113 T08 18MAR2018
113 P28 19JAN2017
113 D95 16JAN2013
113 A01 01MAY2009
551 B12 03APR2011
551 D95 09MAY2015

Any help would be greatly appreciated. Thank you!

1 Answers

using base, since you specified subset

df_2 <- by(df, df$id, function(x) subset(x, any(x$icpc %in% c("D95", "A01"))))
df_2 <- do.call(rbind, lapply(df_2, data.frame))
row.names(df_2) <- NULL

    id icpc  reg.date
1  113  T08 18MAR2018
2  113  P28 19JAN2017
3  113  D95 16JAN2013
4  113  A01 01MAY2009
5  123  D95 19JUN2015
6  123  F85 15AUG2016
7  332  A01 16MAR2010
8  332  A04 20JAN2018
9  332  K20 20FEB2017
10 551  B12 03APR2011
11 551  D95 09MAY2015

using dplyr - much easier in my opinion, but uses filter instead of subset

library(dplyr)
df %>% 
  group_by(id) %>% 
  filter(any(icpc %in% c("D95", "A01")))

      id icpc  reg.date 
   <dbl> <chr> <chr>    
 1   123 D95   19JUN2015
 2   123 F85   15AUG2016
 3   332 A01   16MAR2010
 4   332 A04   20JAN2018
 5   332 K20   20FEB2017
 6   113 T08   18MAR2018
 7   113 P28   19JAN2017
 8   113 D95   16JAN2013
 9   113 A01   01MAY2009
10   551 B12   03APR2011
11   551 D95   09MAY2015
Related