Suppose I have a variable that lasts several periods. Like the number of years when I have an ipod. So, I had the first generation Ipod from 2001 to 2004, and then in 2005 I had Ipod 2 and so on. So my dataframe will look like this:
2001 Ipod1 2002 Ipod1 2003 Ipod1 2004 Ipod1 2005 Ipod2 2006 Ipod2 2007 Ipod2 2008 Ipod2 2009 Ipod3 2010 Ipod3
I want to create a dummy for the period when a new variable will act so that I 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 could 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
In fact, I would like the first element of the dummy to be zero.
My question is: is there a way to make this better?
thanks