Flag first by-group in R data frame -


I have a data frame that looks like this:

  ID score 1 15 1 18 1 16 2 10 293 8 3 47 3 21   

I would like to know one way to flag the first event of an ID - just as before. And the last one. In SAS I have tried a duplicated function, but I really need to add the "flag" column to my data frame because I am running it later through a loop. I want to do something like this:

  ID score first_ind 1 15 1 1 18 0 1 16 0 2 10 1 2 9 3 3 3 1 3 47 0 3 21 0    

  & gt; df $ first_ind & lt; - as.numeric (! Duplicate (df $ id)) & gt; Df ID score first_ind 1 1 15 1 2 1 18 0 3 1 16 0 4 2 10 1 5 2 9 0 6 3 8 1 7 3 47 0 8 3 21 0    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -