1

I have the following data frame:

  PATIENT_ID VALUE
  1          8
  1          16
  1          24 
  2          50 
  2          56
  3          2
  3          70

Now I want to find all PATIENT_IDs that have a minimum that is greater than 48. In this example, the minimum of Patient 1,2,3 are 8, 40 and 2 respectively. So, it should return only PATIENT_ID = 2, since that is the only PATIENT_ID that has a minimum greater than 48.

Peter Lawrence
  • 719
  • 2
  • 10
  • 20

3 Answers3

2
unique(your_df[your_df$VALUE > 48, "PATIENT_ID"])
AidanGawronski
  • 2,055
  • 1
  • 14
  • 24
  • 1
    Note that this is not doing anything "within group". It is just filtering your dataframe to value > 48, selecting PATIENT_ID, and getting unique values. – AidanGawronski Jan 09 '18 at 22:13
1

There are many ways to do this. One way is to first filter out rows that don't match your criteria, and then return the PATIENT_IDs of the remaining rows.

df <- df[df$value > 48,]
df$PATIENT_ID
arvchi
  • 61
  • 11
  • AidanGawronski's answer also takes into account that you only want to report unique values. That is a good idea in your case. – arvchi Jan 09 '18 at 22:20
  • The OP wants `PATIENT_ID` where the minimum `value` is greater than 48. This gives false positives: any `PATIENT_ID` that has *any* value greater than 48, regardless of minimum, gets reported. Using the example data, this will return `PATIENT_ID` 3, even though that `PATIENT_ID` has one row with a value of 2. – Jason Jan 10 '18 at 21:13
-1

Allow for grouping with by:

 by(data = df, INDICES = df$PATIENT_ID, FUN = function(x){ifelse(min(x$VALUE) > 48, min(x$VALUE), FALSE)})

which gives:

df$PATIENT_ID: 1
[1] 0
------------------------------------------------------------------------------------------- 
df$PATIENT_ID: 2
[1] 50
------------------------------------------------------------------------------------------- 
df$PATIENT_ID: 3
[1] 0
Jason
  • 2,507
  • 20
  • 25