find the year when an event occurred of a panel data in R -


have question when dealing data frame constructed panel data,

firm<-c("1","1","1","1","1","2","2","2","3","3","3","3") year<-c("2001","2002","2003","2004","2005","1998","1999","2000","2004","2005","2006","2007") event<-c("yes","yes","yes","no","no","yes","yes","no","yes","no","no","no") df<-data.frame("firm"= firm,"year"= year, "event" = event) 

what want :

firm<-c("1","1","1","1","1","2","2","2","3","3","3","3") year<-c("2001","2002","2003","2004","2005","1998","1999","2000","2004","2005","2006","2007") event<-c("yes","yes","yes","no","no","yes","yes","no","yes","no","no","no") event.year<-c("2004","2004","2004","2004","2004","2000","2000","2000","2005","2005","2005","2005") df<-data.frame("firm"= firm,"year"= year, "event" = event,"event.year" = event.year) 

question how generate column "event.year"?

firm<-c("1","1","1","1","1","2","2","2","3","3","3","3") year<-c("2001","2002","2003","2004","2005","1998","1999","2000","2004","2005","2006","2007") event<-c("yes","yes","yes","no","no","yes","yes","no","yes","no","no","no") df<-data.frame("firm"= firm,"year"= year, "event" = event)  library(plyr) df1 = ddply(df, .(firm), mutate, event.year = year[which(event=="no")[1]]) 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -