This project downloads GICs Industry Group classification for all ASX listed companies, matches to current ASX200 list and outputs Code, Name, Price Market Cap & GICS Industry group to a csv file. Then it aggregates Market Cap by GICS industry group and prints a bar chart.
Step 1: download Complete list from ASX
library("dplyr")
##
## Attaching package: 'dplyr'
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library("XML")
##download ASX data
ThisDir<-getwd()
if(!file.exists("ASX200")) {dir.create("ASX200")}
SavePath<-paste0(ThisDir, "/ASX200")
SaveAs<-paste0(SavePath , "/ASX_List.csv")
fileUrl<-"http://www.asx.com.au/asx/research/ASXListedCompanies.csv"
download.file(fileUrl,destfile=SaveAs)
DateDownloaded<-date()
ASXData <- read.csv(SaveAs, skip=1)
## drop company name here -- will use from ASX200
ASXData$Company.name<-NULL
cat("Data as of",DateDownloaded)
## Data as of Fri Jun 19 19:47:34 2015
Step 2: Download current ASX 200 list with market cap
## download from ASX200 list
fileUrl<-"http://www.marketindex.com.au/asx200"
doc <- htmlParse(fileUrl)
tableNodes <- getNodeSet(doc, "//table")
ASX200Raw <- readHTMLTable(tableNodes[[1]])
## clean up numbers etc and move into tidy dataframe with only ASX200
ASX200Raw[,10]<-gsub('\\$','',ASX200Raw[,10])
ASX200Raw[,10]<-gsub(',','',ASX200Raw[,10])
ASX200Raw$Price<-gsub('\\$','',ASX200Raw$Price)
ASX200List <- data.frame(ASX.Code=ASX200Raw$Code,
Company.Name=ASX200Raw$Company,
Price=as.numeric(ASX200Raw$Price),
Mkt.Cap=as.numeric(ASX200Raw[,10]))
ASX200List$Company.Name <- gsub('Star Stock','',ASX200List$Company.Name)
##Merge in GICS industry groups from ASX file
ASX200List<-left_join(ASX200List,ASXData,by=c("ASX.Code"="ASX.code"))
## Warning in left_join_impl(x, y, by$x, by$y): joining factors with different
## levels, coercing to character vector
write.csv(ASX200List,file=paste0(SavePath,"/ASX200.csv"),row.names=FALSE)
Step 3: Aggregate by Industry Group and print bar chart
ind_mktcap=aggregate(ASX200List$Mkt.Cap, by=list(ASX200List$GICS.industry.group),
sum)
names(ind_mktcap)<-c("GICS","TotMktCap")
ind_mktcap <- ind_mktcap[order(ind_mktcap$TotMktCap),]
library("lattice")
attach(ind_mktcap)
barchart(GICS ~ TotMktCap)