Skip to content

Instantly share code, notes, and snippets.

@renecnielsen
Created September 9, 2013 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renecnielsen/6500025 to your computer and use it in GitHub Desktop.
Save renecnielsen/6500025 to your computer and use it in GitHub Desktop.
### Get, save and load data ### -----
# temp holder file
# create download directory and set it
.exdir = '~/dir'
dir.create(.exdir)
.file = file.path(.exdir, 'MYWorld_votes_all.csv.tar.bz2')
# Download file
url = 'http://ec2-23-22-13-62.compute-1.amazonaws.com/dataset/data/MYWorld_votes_all.csv.tar.bz2'
download.file(url, .file)
# Untar it
untar(.file, compressed = 'bzip2', exdir = path.expand(.exdir))
# Set Working Directory and set dataset
setwd("~/dir/")
myworld <- read.csv("~/dir/MYWorld_votes_all.csv")
# View(myworld)
### Labels ### -----
# Read in labels.
require(RCurl)
countryLabelURL <- getURL("https://docs.google.com/spreadsheet/pub?key=0Agx2NHe4DFa9dFNxUnh4Q204TnNKVFRpcl9obEJSdmc&single=true&gid=1&output=csv")
countryLabels <- read.csv(textConnection(countryLabelURL))
genderLabelURL <- getURL("https://docs.google.com/spreadsheet/pub?key=0Agx2NHe4DFa9dFNxUnh4Q204TnNKVFRpcl9obEJSdmc&single=true&gid=4&output=csv")
genderLabels <- read.csv(textConnection(genderLabelURL))
educationLabelURL <- getURL("https://docs.google.com/spreadsheet/pub?key=0Agx2NHe4DFa9dFNxUnh4Q204TnNKVFRpcl9obEJSdmc&single=true&gid=2&output=csv")
educationLabels <- read.csv(textConnection(educationLabelURL))
prioritiesLabelURL <- getURL("https://docs.google.com/spreadsheet/pub?key=0Agx2NHe4DFa9dFNxUnh4Q204TnNKVFRpcl9obEJSdmc&single=true&gid=3&output=csv")
prioritiesLabels <- read.csv(textConnection(prioritiesLabelURL))
# Add labels
myworld$country <- countryLabels$label[match(myworld$country, countryLabels$id)]
myworld$gender <- genderLabels$label[match(myworld$gender, genderLabels$id)]
myworld$education <- educationLabels$label[match(myworld$education, educationLabels$id)]
myworld$priority1 <- prioritiesLabels$label[match(myworld$priority1, prioritiesLabels$id)]
myworld$priority2 <- prioritiesLabels$label[match(myworld$priority2, prioritiesLabels$id)]
myworld$priority3 <- prioritiesLabels$label[match(myworld$priority3, prioritiesLabels$id)]
myworld$priority4 <- prioritiesLabels$label[match(myworld$priority4, prioritiesLabels$id)]
myworld$priority5 <- prioritiesLabels$label[match(myworld$priority5, prioritiesLabels$id)]
myworld$priority6 <- prioritiesLabels$label[match(myworld$priority6, prioritiesLabels$id)]
### Quick Overview ### -----
head(myworld)
summary(myworld)
### Creating a new variable for each priority ### -----
myworld$climate <- with(myworld, ifelse(priority1 == "Action taken on climate change" |
priority2 == "Action taken on climate change" |
priority3 == "Action taken on climate change" |
priority4 == "Action taken on climate change" |
priority5 == "Action taken on climate change" |
priority6 == "Action taken on climate change", 1, 0))
table(myworld$climate)
myworld$transport <- with(myworld, ifelse(priority1 == "Better transport and roads" |
priority2 == "Better transport and roads" |
priority3 == "Better transport and roads" |
priority4 == "Better transport and roads" |
priority5 == "Better transport and roads" |
priority6 == "Better transport and roads", 1, 0))
table(myworld$transport)
myworld$support <- with(myworld, ifelse(priority1 == "Support for people who can't work" |
priority2 == "Support for people who can't work" |
priority3 == "Support for people who can't work" |
priority4 == "Support for people who can't work" |
priority5 == "Support for people who can't work" |
priority6 == "Support for people who can't work", 1, 0))
table(myworld$support)
myworld$water <- with(myworld, ifelse(priority1 == "Access to clean water and sanitation" |
priority2 == "Access to clean water and sanitation" |
priority3 == "Access to clean water and sanitation" |
priority4 == "Access to clean water and sanitation" |
priority5 == "Access to clean water and sanitation" |
priority6 == "Access to clean water and sanitation", 1, 0))
table(myworld$water)
myworld$health <- with(myworld, ifelse(priority1 == "Better healthcare" |
priority2 == "Better healthcare" |
priority3 == "Better healthcare" |
priority4 == "Better healthcare" |
priority5 == "Better healthcare" |
priority6 == "Better healthcare", 1, 0))
table(myworld$health)
myworld$edu <- with(myworld, ifelse(priority1 == "A good education" |
priority2 == "A good education" |
priority3 == "A good education" |
priority4 == "A good education" |
priority5 == "A good education" |
priority6 == "A good education", 1, 0))
table(myworld$edu)
myworld$government <- with(myworld, ifelse(priority1 == "A responsive government we can trust" |
priority2 == "A responsive government we can trust" |
priority3 == "A responsive government we can trust" |
priority4 == "A responsive government we can trust" |
priority5 == "A responsive government we can trust" |
priority6 == "A responsive government we can trust", 1, 0))
table(myworld$government)
myworld$phone <- with(myworld, ifelse(priority1 == "Phone and internet access" |
priority2 == "Phone and internet access" |
priority3 == "Phone and internet access" |
priority4 == "Phone and internet access" |
priority5 == "Phone and internet access" |
priority6 == "Phone and internet access", 1, 0))
table(myworld$phone)
myworld$energy <- with(myworld, ifelse(priority1 == "Reliable energy at home" |
priority2 == "Reliable energy at home" |
priority3 == "Reliable energy at home" |
priority4 == "Reliable energy at home" |
priority5 == "Reliable energy at home" |
priority6 == "Reliable energy at home", 1, 0))
table(myworld$energy)
myworld$food <- with(myworld, ifelse(priority1 == "Affordable and nutritious food" |
priority2 == "Affordable and nutritious food" |
priority3 == "Affordable and nutritious food" |
priority4 == "Affordable and nutritious food" |
priority5 == "Affordable and nutritious food" |
priority6 == "Affordable and nutritious food", 1, 0))
table(myworld$food)
myworld$forests <- with(myworld, ifelse(priority1 == "Protecting forests, rivers and oceans" |
priority2 == "Protecting forests, rivers and oceans" |
priority3 == "Protecting forests, rivers and oceans" |
priority4 == "Protecting forests, rivers and oceans" |
priority5 == "Protecting forests, rivers and oceans" |
priority6 == "Protecting forests, rivers and oceans", 1, 0))
table(myworld$forests)
myworld$politicalfreedoms <- with(myworld, ifelse(priority1 == "Political freedoms" |
priority2 == "Political freedoms" |
priority3 == "Political freedoms" |
priority4 == "Political freedoms" |
priority5 == "Political freedoms" |
priority6 == "Political freedoms", 1, 0))
table(myworld$politicalfreedoms)
myworld$crime <- with(myworld, ifelse(priority1 == "Protection against crime and violence" |
priority2 == "Protection against crime and violence" |
priority3 == "Protection against crime and violence" |
priority4 == "Protection against crime and violence" |
priority5 == "Protection against crime and violence" |
priority6 == "Protection against crime and violence", 1, 0))
table(myworld$crime)
myworld$discrimination <- with(myworld, ifelse(priority1 == "Freedom from discrimination and persecution" |
priority2 == "Freedom from discrimination and persecution" |
priority3 == "Freedom from discrimination and persecution" |
priority4 == "Freedom from discrimination and persecution" |
priority5 == "Freedom from discrimination and persecution" |
priority6 == "Freedom from discrimination and persecution", 1, 0))
table(myworld$discrimination)
myworld$equality <- with(myworld, ifelse(priority1 == "Equality between men and women" |
priority2 == "Equality between men and women" |
priority3 == "Equality between men and women" |
priority4 == "Equality between men and women" |
priority5 == "Equality between men and women" |
priority6 == "Equality between men and women", 1, 0))
table(myworld$equality)
myworld$job <- with(myworld, ifelse(priority1 == "Better job opportunities" |
priority2 == "Better job opportunities" |
priority3 == "Better job opportunities" |
priority4 == "Better job opportunities" |
priority5 == "Better job opportunities" |
priority6 == "Better job opportunities", 1, 0))
table(myworld$job)
### Save new file as dataframe and .csv ### -----
save(myworld, file="myworld.Rda")
write.csv(myworld, file="myworld.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment