-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData Wrangling.R
426 lines (324 loc) · 11.2 KB
/
Data Wrangling.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# creating an object
object1 <- c(1,2,3,4,5,6,7,8,9,10)
# computing mean
mean(object1)
# computing median
median(object1)
# getting summary stats (minimum, median, mean etc)
summary(object1)
# installing packages
install.packages("tidyverse")
# loading packages
library(tidyverse)
# reading data
dat1 <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
# getting first 6 rows; last 6 rows (tail)
head(dat1)
# getting data structure
str(dat1)
#data types
# integer - whole numbers
# numeric/double - numbers (with decimals)
# character - words (names etc)
# factor - binary
# date
# currency
# check class type
class()
# use the $ to specify a column
mean(dat1$gre) # mean of gre column
summary(dat1$gre) # summary stats of gre column
# pipe ( %>% )
# connect lines of code
# recode the admit column (0-deny, 1-accept) old=new
dat2 <- dat1 %>%
mutate(admit=recode(admit, "0"="deny", "1"="accept"))
# rename columns (rank-position) new=old
dat3 <- dat1 %>%
rename(position=rank)
# load more data
ideal1 <- read.csv("https://raw.githubusercontent.com/cema-uonbi/R_course/main/ideal1.csv")
# data wrangling tasks
## change CaDOB to date format
## clean column names (rename)
## deal with NA in ReasonLoss column
## recode CalfSex column
# recode CalfSex column (1-male, 2-female)
ideal1a <- ideal1 %>%
mutate(CalfSex = recode(CalfSex, "1"="male", "2"="female"))
# check column names
colnames(ideal1)
# clean column names (rename CADOB - calf_date_of_birth)
ideal1b <- ideal1 %>%
rename(calf_date_of_birth = CADOB)
# column names in R (good coding habits)
## calf.date.of.birth
## calf_date_of_birth
# clean column names (janitor)
install.packages("janitor")
library(janitor)
ideal1c <- ideal1%>%
clean_names()
# NA values??
# date formats
## d - 1,2,...,31 (7/8/2023)
## D - 01,02,03,...,31 (07/8/2023)
## m - 1,2,...,12
## M - 01,02,03,...,12
## b - Jan, Feb,...,Dec
## B - January, February,...,March
## y - 21,22,...
## Y - 2021, 2022,...
# 3/1/2008 (d/m/Y)
# 2023-08-07 (Y-M-D)
#install.packages("lubridate") (as_date())
#library(lubridate)
# convert CADOB to date format
ideal1d <- ideal1 %>%
mutate(CADOB = as.Date(CADOB, format="%d/%m/%Y")) %>%
arrange(CADOB) #arrange the data according to the date column
# confirm that CADOB has changed to date format
class(ideal1d$CADOB)
# check sublocations
table(ideal1$sublocation)
# subsetting
# If we wanted data from only one sublocation - Kidera
## using subset
idealkidera <- subset(ideal1, ideal1$sublocation=="Kidera")
## using filter
idealkidera <- ideal1 %>%
filter(sublocation == "Kidera")
# load more data
ideal2 <- read_csv("https://raw.githubusercontent.com/ThumbiMwangi/R_sources/master/ideal2.csv")
# convert VisitDate into date format
ideal2a <- ideal2 %>%
mutate(VisitDate = as.Date(VisitDate, format="%d/%m/%Y"))
# merging datasets
## left join
## right join
## full join
## inner join
ideal3 <- ideal1 %>% left_join(ideal2, by="CalfID")
# subset data using columns (select)
ideal3a <- ideal3 %>%
select(CalfID,VisitID,VisitDate, Theileria.spp.,ELISA_mutans, ELISA_parva)
# pivot (make data longer)
ideal3b <- ideal3a %>%
pivot_longer(cols = c(Theileria.spp., ELISA_mutans, ELISA_parva), names_to = "tests", values_to = "outcome")
# grouping data by CalfID and getting average weight for each calf
ideal3c <- ideal3 %>%
group_by(CalfID) %>%
mutate(avrg_weight = mean(Weight, na.rm = TRUE)) %>%
ungroup()%>%
dplyr::select(CalfID, Weight, avrg_weight)
#load more data
dogdemography <- read_csv("https://raw.githubusercontent.com/cema-uonbi/R_course/main/DogcohortDemographics.csv")
# data wrangling tasks
## rename columns
## format interview date (to date format)
## recode logical variables (0-no, 1-yes)
## get average number of household members per village
## get average number of dogs owned per village
# interviewDate = IntDate # format as date
# householdID = HousehldID # format as character
# villageID = VillageID # format as character (as.character)
# householdMembers = HhMmbrs # format as integer
# ownDogs = OwnDogs # format as logical
# numDogsOwned = DgsOwnd # format as integer (as.numeric)
# adultDogsOwned = AdltDgs # format as integer
# puppiesOwned = Puppies # format as integer
# dogDiedPastMonth = DogDied # format as logical (as.factor)
# numDogsDiedPastMonth = NumDd # format as integer
# dogBitesPastMonth = DogBite # format as logical
dogdemography1 <- dogdemography %>%
rename(interviewDate = IntDate
, householdID = HousehldID
, villageID = VillageID
, householdMembers = HhMmbrs
, ownDogs = OwnDogs
, numDogsOwned = DgsOwnd
, adultDogsOwned = AdltDgs
, puppiesOwned = Puppies
, dogDiedPastMonth = DogDied
, numDogsDiedPastMonth = NumDd
, dogBitesPastMonth = DogBite) %>%
mutate(interviewDate = as.Date(interviewDate, format="%m/%d/%y")) %>%
# 0-no, 1-yes, else-NA
mutate(dogBitesPastMonth = ifelse(dogBitesPastMonth=="0","No", ifelse(dogBitesPastMonth=="1","Yes", NA)))%>%
mutate(villageID = as.character(villageID)) %>%
group_by(villageID) %>%
mutate(avrg_hh_mmbrs=round(mean(householdMembers))) %>%
mutate(avrg_dogs = round(mean(numDogsOwned)))
# load more data
ideal <- read_csv("https://raw.githubusercontent.com/ThumbiMwangi/R_sources/master/ideal3a.csv")
# summarize reasonsLoss
table(ideal$ReasonsLoss1)
library(tidyverse) # data wrangling
install.packages("ggplot2")
library(ggplot2) # plotting
# plot
ggplot(data=ideal, aes(x=ReasonsLoss1)) +
geom_bar() + #specify type of graph
theme_bw() + # change background theme
labs(x="Calf status at end of study",
y="Number of calves",
title = "Graph showing Calf status") # add labels
# get frequency (count)
calves_sublocation<- ideal %>%
select(sublocation) %>%
group_by(sublocation) %>%
count()
# OR (alternatively)
# get frequency (summarise)
calves_sublocation<- ideal %>%
select(sublocation) %>%
group_by(sublocation) %>%
summarise(freq=n())
# plot frequency
ggplot(calves_sublocation, aes(x=sublocation, y=freq))+
geom_col()+
theme_bw()+
labs(x="Sublocation", y="Frequency")
# flip graph
ggplot(calves_sublocation, aes(x=sublocation, y=freq))+
geom_col()+
theme_bw()+
labs(x="Sublocation", y="Frequency")+
coord_flip() # flip graph
# reorder
ggplot(calves_sublocation, aes(x=reorder(sublocation, freq), y=freq))+ # reorder
geom_col()+
theme_bw()+
labs(x="Frequency", y="Sublocation")+
coord_flip() # flip our graph
# get exact number of calves per sublocation (by removing duplicates)
calves_sublocation1 <- ideal %>%
select (CalfID, sublocation) %>%
distinct() %>% # remove duplicates
group_by(sublocation) %>%
summarise(number=n()) %>%
ungroup()
ggplot(calves_sublocation1, aes(x=reorder(sublocation, number), y=number))+
geom_col()+
theme_bw()+
labs(x="Number of calves", y="Sublocation")+
coord_flip()
# summarize by sublocation and gender
calves_gender<- ideal %>%
select(sublocation, CalfSex) %>%
mutate(CalfSex=recode(CalfSex, "1"="Male", "2"="Female")) %>%
group_by(sublocation, CalfSex) %>%
summarise(freq=n()) %>%
ungroup()
# visualise the data using a bar graph
ggplot(calves_gender, aes(x=sublocation, y=freq, fill=CalfSex))+ # color the graph by sex
geom_col()+
theme_bw()+
labs(x="Sublocation", y="Frequency")+
coord_flip()
# customize colors (from colorbrewer website)
ggplot(calves_gender, aes(x=sublocation, y=freq, fill=CalfSex))+ # color the graph by sex
geom_col()+
theme_bw()+
scale_fill_manual(values = c("#fc8d59", "#91bfdb"))+
#scale_fill_brewer(palette = "RdYlBu") +
labs(x="Sublocation", y="Frequency")+
coord_flip()
# summarize by sublocation and gender & get proportions
calves_gender1<- ideal %>%
select(sublocation, CalfSex) %>%
mutate(CalfSex=recode(CalfSex, "1"="Male", "2"="Female")) %>%
group_by(sublocation, CalfSex) %>%
summarise(freq=n()) %>%
ungroup() %>%
group_by(sublocation) %>%
mutate(proportion=freq/sum(freq)*100)
ggplot(calves_gender1, aes(x=sublocation, y=proportion, fill=CalfSex))+ # color the graph by sex
geom_col()+
theme_bw()+
scale_fill_manual(values = c("#fc8d59", "#91bfdb"))+
#scale_fill_brewer(palette = "RdYlBu") +
labs(x="Sublocation", y="Proportion")+
coord_flip()
Mumbua <- 0715256757
# facetting graphs
## have several graphs on same panel
## facet_grid(rows=vars(columnname))
# no color
ggplot(calves_gender, aes(x=sublocation, y=freq))+ # color the graph by sex
geom_col()+
facet_grid(cols = vars(CalfSex))+ #add facets (can also take rows)
theme_bw()+
labs(x="Sublocation", y="Frequency")+
coord_flip()
# add color for male and female
ggplot(calves_gender, aes(x=sublocation, y=freq, fill=CalfSex))+ # color the graph by sex
geom_col()+
facet_grid(cols = vars(CalfSex))+ #add facets (can also take rows)
theme_bw()+
scale_fill_manual(values = c("#fc8d59", "#91bfdb"))+
#scale_fill_brewer(palette = "RdYlBu") +
labs(x="Sublocation", y="Frequency")+
coord_flip()
# one color for all bars
ggplot(calves_gender, aes(x=sublocation, y=freq))+ # color the graph by sex
geom_col(fill="#91bfdb")+
facet_grid(cols = vars(CalfSex))+ #add facets (can also take rows)
theme_bw()+
labs(x="Sublocation", y="Frequency")+
coord_flip()
## facet_wrap(~columnname)
ggplot(calves_gender, aes(x=sublocation, y=freq))+ # color the graph by sex
geom_col(fill="#91bfdb")+
facet_wrap(~CalfSex)+
theme_bw()+
labs(x="Sublocation", y="Frequency")+
coord_flip()
# separate one column into several columns (separate)
dogdemography2 <- dogdemography1 %>%
separate(interviewDate, into = c("year", "month", "day"), sep = "-")
# combine different columns into one column (paste0)
dogdemography3 <- dogdemography2 %>%
mutate(interviewDate=paste0(year,"-",month,"-",day))
# set working directory
setwd("/Users/anitamakori/Library/CloudStorage/Dropbox/CEMA Training Material/DataSets")
# read locally stored data files
# csv
mother_data <- read_csv("L4H_mother_baseline_sample.csv")
# excel
install.packages("readxl")
library(readxl)
objectname <- read_excel("filename.xlsx")
# spss/stata
install.packages("haven")
library(haven)
# dealing with shapefile (geographical) data
# load data
density_data <- read_excel("Density.xlsx")
# clean names
# load shapefile data
county_data <- st_read("County.shp")
# plot map with shapefile data
ggplot(county_data) +
geom_sf(aes(geometry=geometry))
# rename county column in shapefile data
county_data1 <- county_data %>%
rename(County=Name)
# recode counties in data to match shapefile
density_data1 <- density_data %>%
mutate(County=recode(County, "Keiyo-Marakwet"="Elgeyo Marakwet", "Murang'A"="Muranga"))
#join shapefile with density data
density_combined_data <- full_join(county_data1, density_data1, by="County")
#clean column names
# plot maps
ggplot(density_combined_data) +
geom_sf(aes(geometry=geometry, fill=`Density of PHYSICIAN HWF per 10 000 population`))+
theme_void()+
scale_fill_gradient(low = "white", high = "#08306b")
map1 <- ggplot(density_combined_data)+
geom_sf(data=density_combined_data, aes(geometry=geometry), fill=NA)+
geom_sf(aes(fill=`Density of PHYSICIAN HWF per 10 000 population`),color="grey80", size=0.0)+
theme_void()+
scale_fill_gradient(low = "white", high = "#08306b")+
labs(x="", y="",fill="Density of PHYSICIAN HWF\n per 10,000 population")+
theme(text=element_text(size=18))