R function: generate a panel data.table or data.frame to fill with data
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
I have started to work with R and STATA together. I like running regressions in STATA, but I do graphs and setting up the dataset in R. R clearly has a strong comparative advantage here compared to STATA. I was writing a function that will give me a (balanced) panel-structure in R. It then simply works by joining in the additional data.tables or data.frames that you want to join into it.
It consists of two functions:
timeVector <- function(starttime,endtime,timestep="months") { starttime<- as.POSIXct(strptime(starttime, '%Y-%m-%d')) endtime<- as.POSIXct(strptime(endtime, '%Y-%m-%d')) if(timestep=="quarters") { timestep="months" ret<-seq(from=as.POSIXct(starttime), to=as.POSIXct(endtime), by=timestep) quarter <- gsub("(^[123]{1}$)", 1, month(ret)) quarter <- gsub("(^[456]{1}$)", 2, quarter) quarter <- gsub("(^[789]{1}$)", 3, quarter) quarter <- as.numeric(gsub("(^[102]{2}$)", 4, quarter)) ret<-paste(year(ret),quarter,sep="-") ret<-unique(ret) } else { ret<-seq(from=as.POSIXct(starttime), to=as.POSIXct(endtime), by=timestep) } ret }
This first function generates the time-vector, you need to tell it what time-steps you want it to have.
panelStructure <- function(group,timevec) { tt<-rep(timevec,length(group)) tt2 <- as.character(sort(rep(group,length(timevec)))) mat <- cbind("group"=data.frame(tt2),"timevec"=data.frame(tt)) names(mat)<-c("group","timevec") mat }
This second function then generates the panel-structure. You need to give it a group vector, such as for example a vector of district names and you need to pass it the time vector that the other function created.
Hope this is helpful to some of you.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.