r - Dummy variable with time and id condition in long panel -
i'm trying add dummy variable panel data set time, id , many other variables.
library(zoo) geo = c("at","at","at","be","be","be","de","de","de") time = c("1990q1","1990q2","1990q3","1990q1","1990q2","1990q3","1990q1","1990q2","1990q3") data <- as.data.frame(cbind(geo, time)) data$time = as.yearqtr(data$time)
which in reality has 20 countries , 97 quarters. wont around addressing 'geo' element element (time > 2004q1) example great
i want dummy austria , germany starting in 1990 q2. arrive at:
geo time dummmy 1 @ 1990 q1 0 2 @ 1990 q2 1 3 @ 1990 q3 1 4 1990 q1 0 5 1990 q2 0 6 1990 q3 0 7 de 1990 q1 0 8 de 1990 q2 1 9 de 1990 q3 1
i cant anywhere close, i'm thinking in stata logic (generate variable if , else) closest in r create separate country dummies, cbinding each time variable , subsetting them on time variable before extracting single dummies , adding them before cbinding original data. cannot anywhere close best solution (and doesn't work) because around 40 lines of code... should quite easy do, no?
any great!
p.s.: attempts go along these lines:
at <- as.numeric(data$geo == "at") de <- as.numeric(data$geo == "de") @ <- as.data.frame(cbind(data$time, at)) de <- as.data.frame(cbind(data$time, de))
but think i'm off wrong direction , cant time dimension right...
it looks must using zoo
library as.yearqtr
function. if that's case, makes "time" column comparable standard comparison operators. looks want values time>"1990q1" , "geo" either "at" or "de". can with
data$dummy<-(data$time>as.yearqtr("1990q1") & data$geo %in% c("at","de"))+0
here +0
turn logical true/false 0/1
Comments
Post a Comment