generated from jtr13/bookdown-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-Core.Rmd
360 lines (227 loc) · 6.22 KB
/
05-Core.Rmd
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# Working with big data in R
## Read in CSV files
### Read one large CSV file
#### Read a CSV file with header
</center>

</center>
+ [Explanations of important fields in the new attribute-linked residential property price dataset](https://github.com/BINCHI1990/Link-LR-PPD-and-Domestic-EPCs)
```{r}
##It will take around 3 mins to read in 5,732,838 records with 105 variables
tran2<-fread("tranall2011_19.csv")
```
#### Read a CSV file without header
```{r,eval=FALSE}
tran2<-fread("tranall2011_19.csv",header=F)
```
#### Read a CSV file with the second row as header and dropping the first row
<center>

</center>
```{r}
epcdata1<-fread("D_EPC_data_2012_Q4_extract_0221.csv", skip = 1)
epcdata14<-read.csv("D_EPC_data_2020_Q4_extract_0221.csv", skip = 1)
```
### Fast reading multiple EPC csv files together in R
#### Code for reading in EPCs in England and Wales
<center>

</center>
***
<center>

</center>
```{r,eval=FALSE}
## assume all the unzipped EPC stored in EPC folder in D drive
setwd("D:/EPC")
x1 <- list.files(path = ".", pattern = NULL, all.files = FALSE,
full.names = FALSE, recursive = FALSE)
datalist <- paste("D:/EPC",x1,"certificates.csv",sep="/")
epcdata = data.table::rbindlist(lapply(datalist, data.table::fread, showProgress = FALSE))
```
#### Code for reading in EPCs in Scotland
<center>

</center>
```{r,eval=FALSE}
datalist = list.files(pattern="*.csv")
epcdata = data.table::rbindlist(lapply(datalist, data.table::fread, skip=1,showProgress = FALSE))
```
## Basic larger dataset munging/wrangling
### Select columns
```{r}
class(tran2)
needlist<-c("transactionid","postcode","price","dateoftransfer","propertytype","laua","lad11nm","tfarea","priceper","TRANSACTION_TYPE")
tran2<-tran2[,..needlist]
head(tran2)
```
### Changing column names to lower case or upper case
#### Changing column names to lower case
```{r}
setnames(tran2, tolower(names(tran2)))
head(tran2)
```
#### Changing column names to upper case
```{r,eval=FALSE}
setnames(tran2, toupper(names(tran2)))
```
### Filter rows based on conditions
```{r}
tran2[laua=="E09000007", ]
Camden<-tran2[laua=="E09000007", ]
head(Camden)
```
### Add in the ID column
```{r}
Camden[,tranid := .I]
head(Camden)
#Camden[, tranid := .I+1000000]
```
### Convert datatable values to uppercase
```{r}
Camden[, `:=`(tran_type = toupper(transaction_type))]
head(Camden)
```
### Delete a column
```{r}
Camden[,transaction_type:=NULL]
head(Camden)
```
### Remove Duplicates
```{r}
dim(Camden)
unique(Camden)
dim(Camden)
```
### Write files
```{r,eval=FALSE}
fwrite(Camden,"Camden.csv")
```
### Bind datasets
```{r}
# Bind by names
class(epcdata1)
class(epcdata14)
# Convert data.frame to data.table
setDT(epcdata14)
# Select columns
needlist<- c("BUILDING_REFERENCE_NUMBER","OSG_REFERENCE_NUMBER","ADDRESS1","ADDRESS2","ADDRESS3","POSTCODE","INSPECTION_DATE","LODGEMENT_DATE","PROPERTY_TYPE","TYPE_OF_ASSESSMENT","TRANSACTION_TYPE","TOTAL_FLOOR_AREA","NUMBER_HABITABLE_ROOMS","CURRENT_ENERGY_EFFICIENCY","POTENTIAL_ENERGY_EFFICIENCY")
epcdata1<-epcdata1[,..needlist]
epcdata14<-epcdata14[,..needlist]
# Bind by names
l = list(epcdata1,epcdata14)
epc<- rbindlist(l, use.names=TRUE)
# Remove Duplicates
dim(epc)
unique(epc)
dim(epc)
```
## Work with PostGIS database in R
### Write files to PostGIS
```{r,eval=FALSE}
# Loads the PostgreSQL driver
drv <- dbDriver("PostgreSQL")
# Creates a connection to the casa postGIS databas
con <- dbConnect(drv, dbname = "casa",port=5432, user="postgres",password=******)
# Write Camden to the database
dbWriteTable(con, "Camden",value=Camden, append = TRUE, row.names = FALSE)
# Delete some objects from workspace
rm(Camden,tran2,epc,epcdata1,epcdata14,epcdata)
```
### Read files from PostGIS
```{r,eval=FALSE}
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, dbname = "casa",port=5432, user="postgres",password=******)
# load the data from PostGIS database
tran<- dbGetQuery(con,"select * from Camden")
```
## Measure code performance
### Measure running time of the code
```{r}
class(epc)
# Delete the epc object
rm(epc)
# Bind by names
start_time <- Sys.time()
epc<-rbindlist(list(epcdata1,epcdata14),use.names=TRUE)
end_time <- Sys.time()
end_time - start_time
# Convert data.table to data.frame
setDF(epcdata1)
setDF(epcdata14)
# Combine two data frames by rows
start_time <- Sys.time()
epcdata<-rbind(epcdata1,epcdata14)
end_time <- Sys.time()
end_time - start_time
# Convert data.frame to data.table
setDT(epcdata14)
setDT(epcdata1)
# Combine two data tables by position
start_time <- Sys.time()
epc<-rbindlist(list(epcdata1,epcdata14))
end_time <- Sys.time()
end_time - start_time
```
### profvis- an interactive profile visualizations
```{r,eval=FALSE}
profvis({
uniqueresult <- function(x){
dt <- as.data.table(x)
esummary<-dt[,.(count=.N),by=epcdataid]
idd1 <- esummary[esummary$count==1,]
result1 <- x[x$epcdataid %in% idd1$epcdataid,]
return(result1)
}
function1<- function(x,y){
x<-x[is.na(x$saotext),]
x<-x[is.na(x$subbuildingname),]
x$bnstreet <- paste(x$buildingnumber,x$streetdescription,sep=", ")
x$bnstreet <- gsub(" ", "", x$bnstreet)
x$addressf <- paste(x$postcodelocator,x$bnstreet,sep=", ")
y$addressfinal <- trimws(y$add)
y$addressfinal <- gsub(" ", "", y$addressfinal)
y$addressf <- paste(y$postcode,y$addressfinal,sep=", ")
taba1 <- inner_join(x,y,by="addressf")
return(taba1)
}
link1<-function1(add,epc)
link1u<- uniqueresult(link1)
})
```
***
<center>

</center>
***
<center>

</center>
***
<center>

</center>
## Execute R code in Alteryx
<center>

</center>
+ Step 1: build a workflow in Alteryx
<center>

</center>
+ Step 2: create R code in the Alteryx
<center>

</center>
+ Step 3: Click the use AMP engine and run the workflow
<center>

</center>
+ Step 4: Finish
<center>

</center>
<center>

</center>
**Notes:** If you have any questions about Alteryx, [Steven Zhang](https://twitter.com/steven4320555) at [Billigence](https://billigence.com/solutions/business-intelligence/) would like to help.