Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204847

Conditional subset based on one occurrence

$
0
0

I would like to subset a database like this in two parts:

df <- data.frame(ID = c(1,1,1,1,1,2,2,2,3,3,3), 
ins =c(1,2,3,4,5,3,2,4,5,8,9),
Ytx = c(NA,NA,1998,NA,NA,NA,NA,NA,NA,2011,NA))

ID ins  Ytx
  1   1   NA
  1   2   NA
  1   3 1998
  1   4   NA
  1   5   NA
  2   3   NA
  2   2   NA
  2   4   NA
  3   5   NA
  3   8 2011
  3   9   NA

The first should look like this (all the values coming before the occurrence of Ytx grouped by ID):

 ID ins  Ytx
  1   3 1998
  1   4   NA
  1   5   NA
  3   8 2011
  3   9   NA

and the other one made by what is left:

ID ins  Ytx
  1   1   NA
  1   2   NA
  2   3   NA
  2   2   NA
  2   4   NA
  3   5   NA

Thank you for your support


Viewing all articles
Browse latest Browse all 204847

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>