jjk
jjk

Reputation: 3

Create a dummy variable for the first time observation for a unique ID

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.

Upvotes: 0

Views: 274

Answers (1)

Bast_B
Bast_B

Reputation: 143

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

Upvotes: 1

Related Questions