I have a (an unbalanced) panel dataset of individuals over different periods of time (one time period = one month). I'm attempting to analyse the effect on the level of activity of the individuals of opting into two different schemes. I first want to create a subgroup called ControlGroup of individuals who never enter either of the schemes (scheme1= 0 and scheme2= 0) for all the data for those entities.

I've tried using the following code and similar: DF %>% filter(Scheme1==0 & Scheme2==0) -> ControlGroup

However, doing this just creates a subset capturing months for all entities where scheme1 =0 and scheme2 =0. AKA even the entities who do end up entering one or both of the schemes are listed (but only the months in which they haven't yet entered the schemes are listed).

Ideally, what I want for my control group is all the entities who never enter either of the schemes throughout all the data listed for them.



1 Answer 1


You need:

  1. Group by id of individual
  2. Sum how many times that individual appears with each scheme
  3. Filter those resulting 0 from 2.

DF %>% group_by(ID_INDIVIDUAL) %>%mutate(totalSchemes=sum(Scheme1+Scheme2)) %>% filter(totalSchemes==0)

  • 1
    $\begingroup$ Just tried it and worked perfectly, thanks! $\endgroup$
    – EconLiv
    Commented Mar 16, 2019 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.