Andrew O'Geen Andrew O'Geen - 15 days ago 5
R Question

Comparing max and min across groups in R

I have a

data.frame
with nested groups (produced using
summarize
in
dyplr
). Each case contains 2 groups. For each of the two groups, I have a maximum value of another variable and a minimum. So for each case, I have 2 max and 2 min values. What I am trying to do is create a new indicator variable that is coded 1 under specific conditions across groups. Here's some code that produces sample data that look like mine:

data <- expand.grid(groupID=1:2, caseID=1:3)
data$max <- rnorm(nrow(data))
data$min <- rnorm(nrow(data))


So, the basic data look like this:

> data
groupID caseID max min
1 1 1 -0.17317875 -0.65772183
2 2 1 -0.32221584 1.39254577
3 1 2 0.33535437 -1.55013998
4 2 2 -0.03950875 1.24181830
5 1 3 1.00759879 -1.29394582
6 2 3 0.28469228 -0.03454565


What I want is an indicator, unique to each caseID, that is generated by comparing values across groupID. Say, if the the max of group 1 is greater than the min of group 2:

groupID caseID max min newVar
1 1 1 -0.17317875 -0.65772183 0
2 2 1 -0.32221584 1.39254577 0
3 1 2 0.33535437 -1.55013998 0
4 2 2 -0.03950875 1.24181830 0
5 1 3 1.00759879 -1.29394582 1
6 2 3 0.28469228 -0.03454565 1

Answer

You can subset your columns as vectors within groups like this:

library(dplyr)

data %>%
  group_by(caseID) %>%
  mutate(newVar = max[groupID == 1] > min[groupID == 2])
Comments