GeoCenter_DS
9/13/2016 - 4:46 PM

tidyZika.R

a#-------------------------------------------------------------------------------
# Name:		01_ZikaReshape
# Purpose:	Reshape Zika data into a tidy dataframe
# Author:	Tim Essam, Ph.D.
# Created:	2016/09/13
# Owner:	USAID GeoCenter | OakStream Systems, LLC
# License:	MIT License
#-------------------------------------------------------------------------------

# Load required libraries
library(dplyr)
library(readxl) # to read excel data
library(tidyr)

d <- read_excel("Zika_awards.xlsx")

# Unlike in Stata, R can do this all in one pass. 
Zika = d %>% 
  gather(key = "key", value = "Municpality", Muni1:Muni9, na.rm = TRUE) %>% 
  arrange(`Country `, Municpality) %>% 
  select(-key)

# Export the data back into ArcMap world format
write.csv(Zika, file = "Zika_awards_tidy.csv")


# # First, we need to select only the rows that have values for Muni2
# d_sub = d %>% filter(Muni2 != "NA") %>% 
#   select(-Muni1) %>% 
#   gather(key = "key", value = "Municipality", Muni2:Muni9, na.rm = TRUE) %>% 
#   select(-key)
# 
# # Subset the main data frame to retain columns 1 - 4 or (Dept_Tpop - Muni1)
# d_main = d %>% select(Dept_Tpop:Muni1) %>% 
#   filter(Dept_Tpop != "NA") %>% 
#   rename(Municipality = Muni1)
# 
# # Combine the two dataframes together by binding their rows (stacking / appending)
# Zika = bind_rows(d_main, d_sub) %>% 
#   arrange(`Country `, Municipality) 
# 
# # Export the data back into ArcMap world format
# write.csv(Zika, file = "Zika_awards_tidy.csv")