Skip to content

Instantly share code, notes, and snippets.

@vapniks
Created April 11, 2018 03:54
Show Gist options
  • Save vapniks/f76245a4ce3c405a3482f7d0586266cf to your computer and use it in GitHub Desktop.
Save vapniks/f76245a4ce3c405a3482f7d0586266cf to your computer and use it in GitHub Desktop.
convert wide-form Freedom House Good Governance data to long-form
# convert wide-form Freedom House Good Governance data to long-form
# Read the data
wide <- read.csv("freedom_house_good_governance.csv")
# Get the columns corresponding to each wide-form variable that will be converted to long form.
PRvars <- names(wide)[(1:40)*3-1]
CLvars <- names(wide)[(1:40)*3]
Statusvars <- names(wide)[(1:40)*3+1]
# Get the times associated with the wide-form variables
Years <- substr(PRvars,2,5)
# Reshape to long form
long <- reshape(wide,direction="long",varying=list(PRvars,CLvars,Statusvars),idvar="Country",timevar="Year",v.names=c("PR","CL","Status"),times=Years)
# Save the long-form data as a .csv file
write.csv(long,file="freedom_house_good_governance_long.csv",row.names=FALSE,quote=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment