Last active
April 5, 2017 22:33
-
-
Save viveknarang/dcdc83a910bed637233ba3c279d80404 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########################################################### | |
# Data Analytics Assignment A5: R Script for 6/14 A,B,C,D # | |
########################################################### | |
########## Script Written by Vivek Narang 4/4/17 ########## | |
########################################################### | |
# Importing relevant libraries # | |
library(reshape) | |
library(tidyr) | |
library(readr) | |
library(dplyr) | |
library(measurements) | |
# Reference to the Input file for the tasks # | |
file <- "C:\\A5InputFile.csv" | |
# Reading the referenced file for tasks # | |
dataBlockReference <- read_delim( file, col_names = T, delim =',') | |
# Gathering and reshaping the table showing Year and Area Harvested on the table # | |
reshapedTable<- gather(dataBlockReference, 'Year','Area Harvested', 2:8) | |
# Printing the reshaped table # | |
reshapedTable | |
# Checking if the result file exists for task A. If exists skipping the write operation (Error Handling)# | |
if(!file.exists("A5Output_A.csv")){ | |
# If the result file does not exit, write a new result file for task A # | |
write.csv(reshapedTable, "A5Output_A.csv") | |
} | |
# Adding Hectares Column for task B. Computing the Hectares for each based on acres data # | |
reshapedTable$Hectares <- round(conv_unit(reshapedTable$`Area Harvested`, "acre" ,"hectare"), 2); | |
# Printing the modified table on console # | |
reshapedTable | |
# Checking if the result file exists for task B. If exists skipping the write operation (Error Handling)# | |
if(!file.exists("A5Output_B.csv")){ | |
# If the result file does not exit, write a new result file for task B # | |
write.csv(reshapedTable, "A5Output_B.csv") | |
} | |
# Grouping years for computing sum of hectares for each # | |
sumHectares <- reshapedTable %>% group_by(Year) %>% summarise(Hectares = sum(Hectares)) | |
# Printing the data table on console # | |
sumHectares | |
# Checking if the result file exists for task C. If exists skipping the write operation (Error Handling)# | |
if(!file.exists("A5Output_C.csv")){ | |
# If the result file does not exit, write a new result file for task B # | |
write.csv(sumHectares, "A5Output_C.csv") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment