-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTXjobloss.R
111 lines (98 loc) · 3.9 KB
/
TXjobloss.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
txjobloss <- function()
{
#library("devtools")
#install_github('konoanalytics/KonostdlibR')
library("KonostdlibR")
dfjobloss <- gethistory()
dftxcities <- gettxcitygps()
dfjobloss <- merge(dfjobloss, dftxcities,by.x="CITY_NAME",by.y = "city", all.x = TRUE)
dfjobloss$longjitter <- jitter(dfjobloss$long)
dfjobloss$latjitter <- jitter(dfjobloss$lat)
pushtodomo(df=dfjobloss,datasetname="TX WARN Notifications", api_key=KonostdlibR::getcredentials("Domo"))
}
pushtodomo <- function(df, datasetname, api_key=NULL, organization="KonoAnalytics", createdataset=FALSE)
{
#library("devtools")
#install_github('konoanalytics/KonostdlibR')
library("KonostdlibR")
library("DomoR")
api_key <- as.character(KonostdlibR::getcredentials("Domo")$api_key)
DomoR::init(organization,api_key)
if(createdataset)
{
DomoR::create(df, name=datasetname)
}else
{
DataSetInfo <- DomoR::list_ds(name=datasetname)
replace <- DomoR::replace_ds(DataSetInfo$id, df)
}
}
gethistory <- function()
{
library("xlsx")
library("plyr")
#http://www.twc.state.tx.us/businesses/worker-adjustment-and-retraining-notification-warn-notices
destpath <- "~/jobloss/"
dfcum <- data.frame()
for (i in 2004:2013)
{
url <- paste0("http://www.twc.state.tx.us/files/news/warn-act-listings-",i,".xls")
destfile <- paste0(destpath,i,".xls")
download.file(url,destfile = destfile, mode = "wb")
if (i == 2010)
{
#first row is not headers in 2010 alone
df <- read.xlsx(paste0(destpath,i,".xls"),sheetIndex = 1,startRow = 2)
df <- df[,1:8]
names(df) <- c("NOTICE_DATE","JOB_SITE_NAME","COUNTY_NAME","WDA_NAME","TOTAL_LAYOFF_NUMBER",
"LayOff_Date","WFDD_RECEIVED_DATE","CITY_NAME")
dfcum <- rbind(dfcum, df)
}else
{
df <- read.xlsx(paste0(destpath,i,".xls"),sheetIndex = 1)
df <- df[,1:8]
names(df) <- c("NOTICE_DATE","JOB_SITE_NAME","COUNTY_NAME","WDA_NAME","TOTAL_LAYOFF_NUMBER",
"LayOff_Date","WFDD_RECEIVED_DATE","CITY_NAME")
dfcum <- rbind(dfcum, df)
}
}
#changed to xlsx format in 2014
for (i in 2014:2015)
{
url <- paste0("http://www.twc.state.tx.us/files/news/warn-act-listings-",i,".xlsx")
destfile <- paste0(destpath,i,".xlsx")
download.file(url,destfile = destfile, mode = "wb")
df <- read.xlsx(paste0(destpath,i,".xlsx"),sheetIndex = 1)
df <- df[,1:8]
names(df) <- c("NOTICE_DATE","JOB_SITE_NAME","COUNTY_NAME","WDA_NAME","TOTAL_LAYOFF_NUMBER",
"LayOff_Date","WFDD_RECEIVED_DATE","CITY_NAME")
dfcum <- rbind(dfcum, df)
}
#introduced new fields in 2016
i <- 2016
url <- paste0("http://www.twc.state.tx.us/files/news/warn-act-listings-",i,".xlsx")
destfile <- paste0(destpath,i,".xlsx")
download.file(url,destfile = destfile, mode = "wb")
df <- read.xlsx(paste0(destpath,i,".xlsx"),sheetIndex = 1)
dfcum <- rbind.fill(dfcum, df)
dfcum$layoffyear <- as.numeric(format(dfcum$LayOff_Date,"%Y"))
dfcum$TOTAL_LAYOFF_NUMBER <- as.numeric(dfcum$TOTAL_LAYOFF_NUMBER)
dfcum
}
gettxcitygps <- function(fileandpath="~/jobloss/txcities.xlsx",uploaddomo=TRUE)
{
library("xlsx")
df <- read.xlsx(fileandpath,sheetIndex = 1)
df <- df[,c("city","lat","long")]
df$city <- as.character(df$city)
df$classification <- NA
df$classification[grep(" city",df$city)] <- "city"
df$classification[grep(" town",df$city)] <- "town"
df$city[grep(" city",df$city)] <- gsub(" city","",df$city[grep(" city",df$city)])
df$city[grep(" town",df$city)] <- gsub(" town","",df$city[grep(" town",df$city)])
if(uploaddomo)
{
pushtodomo(df,datasetname="GPS of Texas Cities")
}
df
}