Home > Mobile >  Create a dummy variable for the first time observation for a unique ID
Create a dummy variable for the first time observation for a unique ID

Time:10-13

I´m handeling a panel data frame with a list of unique names and dates of recording. I want to create a dummy variable for the first time a name is recorded - i.e. a dummy that takes the value 1 the first time a name observation is recorded, and 0 for the second, third ... time.

Any help would be appreciated.

CodePudding user response:

Maybe you could use combine from gdata package. It merges your dataframe into one keeping a column to identify them. After that you could do some count using dplyr, something like that

newdf=gdata::combine(df1,df2)
newdf%>%group_by(your_variable_of_interest)%>%mutate(count_index= row_number())

From that you will a the number of occurence from 1 to n and you will just have to replace n>1 by 0

Would it work for you ? Otherwise we will need a reproducible example

  •  Tags:  
  • r
  • Related