-
Notifications
You must be signed in to change notification settings - Fork 15
/
prep ocsi data - scotland.r
59 lines (44 loc) · 1.67 KB
/
prep ocsi data - scotland.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
library(tidyverse)
library(readxl)
library(janitor)
# Read in OSCI data
ocsi_sco = read_excel("data/OCSI/Scotland_COVID_19_Dataset_IMZ.xlsx",
col_names = FALSE)
ocsi_sco = ocsi_sco %>%
janitor::remove_empty("cols")
# Change cell values to get a row of consistent variable names
ocsi_sco[3,1] <- ocsi_sco[8,1]
ocsi_sco[3,2] <- ocsi_sco[8,2]
# Replace NA values with empty strings so string concatenation doesn't fail in later step
ocsi_sco[6,1] <- ""
ocsi_sco[6,2] <- ""
# Remove metadata
ocsi_sco <- ocsi_sco %>% slice(c(-1, -2, -4, -5, -7, -8))
# drop Excel "LINK" columns
# ocsi_sco = ocsi_sco[,-c(20, 35, 48, 71, 106)]
# Combine first two rows to create new col names
column_names <- str_c(str_replace_na(ocsi_sco[1,]), str_replace_na(ocsi_sco[2,]), sep = " ")
# Rename columns
names(ocsi_sco) <- column_names
# Remove first two rows used to create new column names and
# COVID-19 column
ocsi_sco <- ocsi_sco %>%
slice(-1:-2) %>%
select(-`COVID-19 vulnerability index Score`)
# Rename Aged columns to make variables clear
ocsi_sco <- ocsi_sco %>%
rename_at(vars(starts_with("Aged")), ~ str_c("Proportion of Population ", .))
# Remove whitespace and convert MSOA Code to upper case
ocsi_sco <- ocsi_sco %>%
rename(Code = `Intermediate Zone Code `) %>%
mutate(Code = str_to_upper(Code)) %>%
select(-`Intermediate Zone Name `)
# Convert all columns to numeric except Code
ocsi_sco_code <- ocsi_sco %>%
select(Code)
ocsi_sco_numeric <- ocsi_sco %>%
select(-Code) %>%
mutate_if(is.character, as.numeric)
ocsi_sco <- bind_cols(ocsi_sco_code,
ocsi_sco_numeric)
rm(ocsi_sco_code, ocsi_sco_numeric, column_names)