Skip to content

Instantly share code, notes, and snippets.

@cavedave
Last active April 16, 2020 10:15
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 cavedave/bf63fff36555fd884429384125012afc to your computer and use it in GitHub Desktop.
Save cavedave/bf63fff36555fd884429384125012afc to your computer and use it in GitHub Desktop.
historic<-read.csv("historic.csv", skip=10)
library(dplyr)
# Drop the columns of the dataframe
historic<-select (historic,-c(X.1,X.2,X.3,X.4,X.5,X.6,X.7,X.8,X.9,X.10,X.11))
historic <- rename(historic, number = Level..numbers.of.unemployed.people.) #For renaming dataframe column
historic <- rename(historic, rate = Rate....) #For renaming dataframe column
#start is nas
historic2<-slice(historic, 499:n())
#regex to put date in right format
historic2<-historic2 %>%
mutate(X = gsub("Jan ","01/01/",X))
historic2<-historic2 %>%
mutate(X = gsub("Feb ","01/02/",X))
historic2<-historic2 %>%
mutate(X = gsub("Mar ","01/03/",X))
historic2<-historic2 %>%
mutate(X = gsub("Apr ","01/04/",X))
historic2<-historic2 %>%
mutate(X = gsub("May ","01/05/",X))
historic2<-historic2 %>%
mutate(X = gsub("Jun ","01/06/",X))
historic2<-historic2 %>%
mutate(X = gsub("Jul ","01/07/",X))
historic2<-historic2 %>%
mutate(X = gsub("Aug ","01/08/",X))
historic2<-historic2 %>%
mutate(X = gsub("Sep ","01/09/",X))
historic2<-historic2 %>%
mutate(X = gsub("Oct ","01/10/",X))
historic2<-historic2 %>%
mutate(X = gsub("Nov ","01/11/",X))
historic2<-historic2 %>%
mutate(X = gsub("Dec ","01/12/",X))
#make numbers numbers
historic2<-historic2 %>%
mutate(number = gsub(",","",number))
library(lubridate)
historic2<-historic2 %>%
dplyr::mutate(year = lubridate::year(X)
historic2$number<-as.numeric(historic2$number)
historic2<-historic2 %>%
mutate(date= dmy(X))
historic2<-historic2 %>%
mutate(date2=decimal_date(date))
#plot to sanity check
ggplot(historic2, aes(x=date2, y=number, group=1)) +
geom_line()
#load recent data
recent<-read.csv("recent.csv", skip=10, header = FALSE)
recent<-select (recent,-c(V2))
recent <- rename(recent, date = V1) #For renaming dataframe column
#recent <- rename(recent, nothing = V2) #For renaming dataframe column
recent <- rename(recent, number = V3)
historic2<-select (historic2,-c(X,rate,date,'date2 <- decimal_date(date)'))
head(historic2)
#fix dates again
recent2<-recent2 %>%
mutate(date = gsub(" Jan","/01/01",date))
recent2<-recent2 %>%
mutate(date = gsub(" Feb","/02/01",date))
recent2<-recent2 %>%
mutate(date = gsub(" Mar","/03/01",date))
recent2<-recent2 %>%
mutate(date = gsub(" Apr","/04/01",date))
recent2<-recent2 %>%
mutate(date = gsub(" May","/05/01",date))
recent2<-recent2 %>%
mutate(date = gsub(" Jun","/06/01",date))
recent2<-recent2 %>%
mutate(date = gsub(" Jul","/07/01",date))
recent2<-recent2 %>%
mutate(date = gsub(" Aug","/08/01",date))
recent2<-recent2 %>%
mutate(date = gsub(" Sep ","/09/01",date))
recent2<-recent2 %>%
mutate(date = gsub(" Oct","/10/01",date))
recent2<-recent2 %>%
mutate(date = gsub(" Nov","/11/01",date))
recent2<-recent2 %>%
mutate(date = gsub(" Dec","/12/01",date))
recent2<-recent2 %>%
mutate(date= ymd(date))
recent2<-recent2 %>%
mutate(date2=decimal_date(date))
#clean up
recent2<-select (recent2,-c(date))
recent2<-recent2 %>%
mutate(number= number*1000)
#put dataframes together
total <- rbind(historic2,recent2)
#find empties
which(is.na(total))
#add new data not yet in dataset
#https://www.poundsterlinglive.com/economics/13056-uk-unemployment-rate-left-at-highest-since-june-2014-by-dwp-s-950k-new-claims
total[nrow(total) + 1,] = c(2167100,2020.247)
#turn this into monthly changes
total2<-total %>%
mutate(D = number- lag(number))
total2<-slice(total2, 2:n())
#graph it
ggplot(total2, aes(x=date2, y=D, group=1)) +
geom_line()+ theme_minimal()+
ggtitle("Monthly Changes in UK Unemployment")+ labs(y="Number of People", x = "Date")+
theme(plot.title = element_text(hjust = 0.5),
plot.margin = margin(10, 10, 10, 10))
ggsave("unemployedChange.png")
@cavedave
Copy link
Author

EVqhNiVXYAEO6Sa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment