Suppose I have a variable that lasts for several periods.
Like the amount of years that I have an Ipod.
So I had the Ipod 1st generation from 2001 until 2004 and then in 2005 I’ve got Ipod 2 and so on. So my dataframe would look like:
2001 Ipod1
2002 Ipod1
2003 Ipod1
2004 Ipod1
2005 Ipod2
2006 Ipod2
2007 Ipod2
2008 Ipod2
2009 Ipod3
2010 Ipod3
What I want is to create a dummy for the period when a new variable arrives so I would get:
Year Var Dummy
2001 Ipod1 1
2002 Ipod1 0
2003 Ipod1 0
2004 Ipod1 0
2005 Ipod2 1
2006 Ipod2 0
2007 Ipod2 0
2008 Ipod2 0
2009 Ipod3 1
2010 Ipod3 0
So far I have been able to do this:
df = structure(list(Year = 2001:2010, Var = structure(c(1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 3L, 3L), .Label = c("Ipod1", "Ipod2", "Ipod3"
), class = "factor")), .Names = c("Year", "Var"), class = "data.frame", row.names = c(NA,
-10L))
df$number.in.group = unlist(lapply(table(df$Var),seq.int))
df$dummy = ifelse(df$number.in.group == 1,1,0)
df$dummy[1]=0
Actually I would like the first element of the dummy to be zero.
My question is: Is there any way of doing this in a better way?
Thanks
How about this: