-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.Rhistory
473 lines (473 loc) · 16.2 KB
/
.Rhistory
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
library(tidyr)
library(tidyselect)
library(tidyverse)
library(ggthemes)
library(nycflights13)
Sys.setenv(LANG = "eng")
source('C:/Users/david/Desktop/R/R-for-data-science.R', echo=TRUE)
source('C:/Users/david/Desktop/R/R-for-data-science.R', echo=TRUE)
source('C:/Users/david/Desktop/R/R-for-data-science.R', echo=TRUE)
source('C:/Users/david/Desktop/R/R-for-data-science.R', echo=TRUE)
source('C:/Users/david/Desktop/R/R-for-data-science.R', echo=TRUE)
source('C:/Users/david/Desktop/R/R-for-data-science.R', echo=TRUE)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg)
ggplot(data = mpg) +
geom_point(mapping = aes(x = hwy, y = cyl))
source('C:/Users/david/Desktop/R/R-for-data-science.R', echo=TRUE)
ggplot(data = mpg) +
geom_point(mapping = aes(x = hwy, y = cyl))
ggplot(data = mpg) +
geom_point(mapping = aes(x = hwy, y = cyl))
library(tidyr)
library(tidyselect)
library(tidyverse)
library(ggthemes)
library(nycflights13)
Sys.setenv(LANG = "eng")
ggplot(data = flights, mapping = aes(dep_time, arr_delay)) +
geom_point()
library(tidyr)
library(tidyselect)
library(tidyverse)
library(ggthemes)
library(nycflights13)
Sys.setenv(LANG = "eng")
str(flights)
min_delay_daytime <- flights %>%
ggplot(data = flights, mapping = aes(dep_time, arr_delay)) +
geom_point() +
geom_line()
ggplot(data = flights, mapping = aes(dep_time, arr_delay)) +
geom_point() +
geom_line()
count = n(),
avg_arr_delay = mean(arr_delay, na.rm = TRUE)) %>%
arrange(desc(avg_arr_delay)))
(min_delay_daytime <- flights %>%
group_by(hour) %>%
summarise(
count = n(),
avg_arr_delay = mean(arr_delay, na.rm = TRUE)) %>%
arrange(desc(avg_arr_delay)))
?n
(min_delay_daytime <- flights %>%
group_by(hour) %>%
summarise(
count = n(),
avg_arr_delay = mean(arr_delay, na.rm = TRUE)) %>%
filter(n > 30)
arrange(desc(avg_arr_delay)))
(min_delay_daytime <- flights %>%
group_by(hour) %>%
summarise(
count = n(),
avg_arr_delay = mean(arr_delay, na.rm = TRUE)) %>%
filter(count > 30)
arrange(desc(avg_arr_delay)))
(min_delay_daytime <- flights %>%
group_by(hour) %>%
summarise(
count = n(),
avg_arr_delay = mean(arr_delay, na.rm = TRUE)) %>%
filter(!is.na(count))
arrange(desc(avg_arr_delay)))
(min_delay_daytime <- flights %>%
group_by(hour) %>%
summarise(
count = n(),
avg_arr_delay = mean(arr_delay, na.rm = TRUE)) %>%
filter(count > 0)
arrange(desc(avg_arr_delay)))
(min_delay_daytime <- flights %>%
group_by(hour) %>%
summarise(
count = n(),
avg_arr_delay = mean(arr_delay, na.rm = TRUE)) %>%
filter(count > 0)
arrange(desc(avg_arr_delay)))
(min_delay_daytime <- flights %>%
group_by(hour) %>%
summarise(
count = n(),
avg_arr_delay = mean(arr_delay, na.rm = TRUE)) %>%
filter(count > 0)
arrange(desc(avg_arr_delay)))
(min_delay_daytime <- flights %>%
group_by(hour) %>%
summarise(
count = n(),
avg_arr_delay = mean(arr_delay, na.rm = TRUE)) %>%
filter(count > 0) %>%
arrange(desc(avg_arr_delay)))
(min_delay_daytime <- flights %>%
group_by(hour) %>%
summarise(
count = n(),
avg_arr_delay = mean(arr_delay, na.rm = TRUE)) %>%
filter(count > 0) %>%
arrange(avg_arr_delay))
View(combined_matrix)
View(diamonds2)
View(cars.subset)
search()
systemInfo()
systemInfo()
system("info")
system()
?system
search()
diamonds
diamonds2
View(diamonds2)
diamonds2
Sys.setenv(lang = "eng")
aetrsoin
diamonds2
library("vctrs")
diamonds
data()
swiss
attenu
?data
WorldPhones
cars
chickwts
co2
co2
head(co2)
str(co2)
library(dplyr)
glimpse(co2)
Nile
Titanic
str(Titanic)
glimpse(Titanic)
big_matrix
library(ggplot2)
data()
co2
chickwt
Nile
rock
mpg
ggplot(mpg, aes(cty, hwy))
ggplot(mpg, aes(cty, hwy)) +
geom_point(aes(label = drv))
geom_point(aes(drv = label))
ggplot(mpg, aes(cty, hwy)) +
geom_text(aes(label = year))
diamonds
head(data.frame(ntoken(sentiment_corpus)))
head(data.frame(ntoken(sentiment_corpus)))
head(data.frame(ntoken(sentiment_corpus)))
test_vector1
x <- c(test_vector1[1,2])
x <- test_vector1[c(1,2)]
x
?rm
rm(list=ls())
library(haven)
library(tidyverse)
library(margins)
library(magrittr)
library(ggeffects)
library(lme4)
library(splines)
library(stargazer)
library(dplyr)
library(tidytext)
library(strex)
library(tm)
library(stringr)
## Data Preparation
#Import the dataset. Filter for variables that we need in the end as well as rename the variables to facilitate subsequent coding.
hudoc <- read.csv("hudoc.csv")
head(hudoc)
hudoc <- hudoc %>% rename(case = Document.Title,
case_id = Application.Number,
concdate = Date,
conclusion = Conclusion) %>% select(-3, -4)
#Recode and create variables
#convert everything to lower case to facilitate recoding
hudoc$conclusion <- sapply(hudoc$conclusion, tolower)
hudoc$case <- sapply(hudoc$case, tolower)
#create country variable
hudoc$case <- gsub("v. |c. ", "vs.", hudoc$case)
hudoc$country <- gsub(".*vs.", "", hudoc$case)
hudoc$country <- word(hudoc$country, 1)
table(hudoc$country)
hudoc$country <- gsub("slovaquie", "slovakia", hudoc$country)
hudoc$country <- gsub("suisse", "switzerland", hudoc$country)
?ylim
ggplot2::ylim
?ggplot2::ylim
library(ggplot2)
?ylim
nchar("hi")
length("hi")
lengths("hi")
search()
library(haven)
library(tidyverse)
library(margins)
library(magrittr)
search()
?matrix
matrix(1:9)
matrix(1:9, nrow = 3)
matrix(1:9, nrow = 3)[c(1,3),1]
data()
morley
?morley
trees
women
women
women
glimpse(women)
economics
tail(economics)
ggplot(economics, aes(date, unemploy))
ggplot(economics, aes(date, unemploy)) +
geom_line()
ggplot(economics) +
geom_line(aes(date, unemploy)) +
geom_line(aes(date, pop))
ggplot(economics) +
geom_line(aes(date, pop/unemploy))
ggplot(economics) +
geom_line(aes(date, unemploy/pop))
ggplot(economics, aes(date, unemploy)) +
geom_line()
ggplot(economics) +
geom_line(aes(date, unemploy/pop))
install.packages(scales)
install.packages("scales")
library(sclaes)
install.packages("scales")
library(scales)
ggplot(economics) +
geom_line(aes(date, scales::percent(unemploy/pop)))
library(ggplot2)
ggplot(economics) +
geom_line(aes(date, scales::percent(unemploy/pop)))
ggplot(economics) +
geom_line(aes(date, percent(unemploy/pop)))
ggplot(economics) +
geom_line(aes(date, unemploy/pop))
economics
?economics
ggplot(economics) +
geom_line(aes(date, psavert))
ggplot(economics) +
geom_line(aes(date, unemploy/pop)) +
xlim(c(1990, 2000))
ggplot(economics) +
geom_line(aes(date, unemploy/pop)) +
xlim(c(as.Date(1990, 2000)))
#this clears the environment
rm(list = ls())
#this sets the wd to the directory of this R file. might not be necessary, but better be safe than sorry
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
#sets R to english because I don't like german warnings
Sys.setenv(LANG = "en")
library(haven)
library(tidyverse)
library(margins)
library(magrittr)
library(ggeffects)
library(lme4)
library(splines)
library(stargazer)
library(dplyr)
library(tidytext)
library(strex)
library(tm)
library(stringr)
## Data Preparation
#Import the dataset. Filter for variables that we need in the end, rename the variables to facilitate subsequent coding, and remove duplicates.
hudoc <- read.csv("hudoc.csv")
head(hudoc)
hudoc <- hudoc %>% rename(case = Document.Title ,case_id = Application.Number,
concdate = Date,
conclusion = Conclusion)%>% select(-3, -4) %>% distinct(case_id, .keep_all = TRUE)
#Recode and create variables
#convert everything to lower case to facilitate recoding
hudoc$conclusion <- sapply(hudoc$conclusion, tolower)
hudoc$case <- sapply(hudoc$case, tolower)
#create country variable
hudoc$case <- gsub("v. |c. ", "vs.", hudoc$case)
hudoc$country <- gsub(".*vs.", "", hudoc$case)
hudoc$country <- word(hudoc$country, 1)
table(hudoc$country)
hudoc$country <- gsub("suisse", "switzerland", hudoc$country)
#create violation variable (first filter for the text and number of article separately, then merge together. Afterwards remove junk/summarize)
#create the text variable for the extraction of violations
#hudoc$text <- str_extract(hudoc$conclusion, "^\\D+")
#hudoc$text <- rm()
hudoc$text <- hudoc$conclusion
#removeSpecialChars <- function(x) gsub("[^a-zA-Z0-9\\+ ]", " ", x)
#hudoc$text <- sapply(hudoc$text, removeSpecialChars)
#this makes it so that the french is the same as english
hudoc$text <- gsub(" | ", " ", hudoc$text)
hudoc$text <- gsub(" de l'", " of ", hudoc$text)
hudoc$text <- gsub("article ", "art. ", hudoc$text)
hudoc$text <- gsub("non ", "no ", hudoc$text)
hudoc$text <- gsub("non-violation", "no violation", hudoc$text)
hudoc$text <- gsub("\\+", " violation of art. ", hudoc$text)
#this looks for the phrase "violation of" followed by any number of non-digit characters and ends with one or more digits. the whole phrase must not be preceded by "no ". the matches are then written into the new varialbe "violations_with_text". the variable is a list
hudoc$violations_with_text <- stringr::str_extract_all(hudoc$text, "(?<!no\\s)violation\\sof\\sart\\D{0,20}\\d+(?!\\sof\\sprotocol)")
# #this creates a new variable called "violations_with_text". it gets rid of the text and leaves only the numbers. I couldn't do it with only regex because for some reason, using negative and positive lookbehind does not work (in R). anyway, the variable is also a list
# for (l in 1:length(hudoc$violations_with_text)) {
# hudoc$violations_with_text_numbers[l] <- stringr::str_extract_all(hudoc$violations_with_text[l], "\\d+")
# }
#this loop below gets rid of duplicate values from situations like "violation of art. 14+3" where 14 was matched twice
for (l in 1:length(hudoc$violations_with_text)) {
hudoc$violations_with_text[[l]] <- unique(hudoc$violations_with_text[[l]])
}
#this gets rid of rows where "hudoc$violations_with_text" is missing values
hudoc <- hudoc[!is.na(hudoc$violations_with_text), ]
#still to do before data visualization:
# - find and get rid of duplicates (is included in the first lines of code)
# - use hudoc$violations variable to create a new row in every case where there is more than one violation, copying everything except for the value of hudoc$violation. don't know how to do that yet
# => I was a bit lazy and chose another approach, I groupd the violations (violations_with_text) by country, aggregated all violations per country, got rid of the "violations of" part, and extracted the numbers
#create new dataobject and group new dataset "bycountry" by country, remove list values by applying unlist
bycountry <- apply(hudoc, 2, function(y) sapply(y, function(x) paste(unlist(x), collapse=" ")))
#aggregate data by country, concatenate text of violations
agg <- aggregate(data=bycountry, unlist(violations_with_text)~country, paste0, collapse=' ')
#rename variable name to concat_violations
agg$concat_violations <-agg$`unlist(violations_with_text)`
#get number of violations for slovakia (since the values are displayed as a list, we need to apply unlist; in a second step we remove the spaces)
slovakia <- agg$concat_violations[1]
slovakia <- str_split(slovakia, 'violation of art. ')
slovakia <- unlist(slovakia)
slovakia <- gsub(" | ", "", slovakia)
#not necessary, but facilitates the identification of articles (see levels)
slovakia <- as.factor(slovakia)
slovakia
#art 6 is the most violated article for Slovakia
table(slovakia)
#get number of violations for switzerland (since the values are displayed as a list, we need to apply unlist; in a second step we remove the spaces)
switz <- agg$concat_violations[2]
switz <- str_split(switz, 'violation of art. ')
switz <- unlist(switz)
switz <- gsub(" | ", "", switz)
#not necessary, but facilitates the identification of articles (see levels)
switz <- as.factor(switz)
switz
#Interestingly, art 6 is the most violated article for Switzerland
table(switz)
#this clears the environment
rm(list = ls())
#this sets the wd to the directory of this R file. might not be necessary, but better be safe than sorry
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
#sets R to english because I don't like german warnings
Sys.setenv(LANG = "en")
library(haven)
library(tidyverse)
library(margins)
library(magrittr)
library(ggeffects)
library(lme4)
library(splines)
library(stargazer)
library(dplyr)
library(tidytext)
library(strex)
library(tm)
library(stringr)
## Data Preparation
#Import the dataset. Filter for variables that we need in the end, rename the variables to facilitate subsequent coding, and remove duplicates.
hudoc <- read.csv("hudoc.csv")
head(hudoc)
hudoc <- hudoc %>% rename(case = Document.Title ,case_id = Application.Number,
concdate = Date,
conclusion = Conclusion)%>% select(-3, -4) %>% distinct(case_id, .keep_all = TRUE)
#convert everything to lower case to facilitate recoding
hudoc$conclusion <- sapply(hudoc$conclusion, tolower)
hudoc$case <- sapply(hudoc$case, tolower)
#create country variable
hudoc$case <- gsub("v. |c. ", "vs.", hudoc$case)
hudoc$country <- gsub(".*vs.", "", hudoc$case)
hudoc$country <- word(hudoc$country, 1)
table(hudoc$country)
hudoc$country <- gsub("suisse", "switzerland", hudoc$country)
#create text variable as a first step to retract violated articles
hudoc$text <- hudoc$conclusion
#this makes it so that the french is the same as english
hudoc$text <- gsub(" | ", " ", hudoc$text)
hudoc$text <- gsub(" de l'", " of ", hudoc$text)
hudoc$text <- gsub("article ", "art. ", hudoc$text)
hudoc$text <- gsub("non ", "no ", hudoc$text)
hudoc$text <- gsub("non-violation", "no violation", hudoc$text)
hudoc$text <- gsub("\\+", " violation of art. ", hudoc$text)
#this looks for the phrase "violation of" followed by any number of non-digit characters and ends with one or more digits. the whole phrase must not be preceded by "no ". the matches are then written into the new varialbe "violations_with_text". the variable is a list
hudoc$violations_with_text <- stringr::str_extract_all(hudoc$text, "(?<!no\\s)violation\\sof\\sart\\D{0,20}\\d+(?!\\sof\\sprotocol)")
#this loop below gets rid of duplicate values from situations like "violation of art. 14+3" where 14 was matched twice
for (l in 1:length(hudoc$violations_with_text)) {
hudoc$violations_with_text[[l]] <- unique(hudoc$violations_with_text[[l]])
}
#this gets rid of rows where "hudoc$violations_with_text" is missing values
#hudoc <- hudoc[!is.na(hudoc$violations_with_text), ]
#hudoc <- list.clean(hudoc, fun = function(x) length(x) == 0L, recursive = TRUE)
#create new dataobject and group new dataset "bycountry" by country, remove list values by applying unlist
bycountry <- apply(hudoc, 2, function(y) sapply(y, function(x) paste(unlist(x), collapse=" ")))
#aggregate data by country, concatenate text of violations
agg <- aggregate(data=bycountry, unlist(violations_with_text)~country, paste0, collapse=' ')
#rename variable name to concat_violations
agg$concat_violations <-agg$`unlist(violations_with_text)`
#get number of violations for slovakia (since the values are displayed as a list, we need to apply unlist; in a second step we remove the spaces)
slovakia <- agg$concat_violations[1]
slovakia <- str_split(slovakia, 'violation of art. ')
slovakia <- unlist(slovakia)
slovakia <- gsub(" | ", "", slovakia)
#not necessary, but facilitates the identification of articles (see levels)
slovakia <- as.factor(slovakia)
slovakia
#art 6 is the most violated article for Slovakia
table(slovakia)
#get number of violations for switzerland (since the values are displayed as a list, we need to apply unlist; in a second step we remove the spaces)
switz <- agg$concat_violations[2]
switz <- str_split(switz, 'violation of art. ')
switz <- unlist(switz)
switz <- gsub(" | ", "", switz)
#not necessary, but facilitates the identification of articles (see levels)
switz <- as.factor(switz)
switz
#Interestingly, art 6 is the most violated article for Switzerland
table(switz)
bycountry
glimpse(bycountry)
str(bycountry)
function()
?function
?function
aggregate
?aggregate
?try
ggplot(data = )
matrix(1:9, 1:8)
matrix(himark)
try(matrix(himark))
library("readr")
parse_number(c("this is number 1", "hi mark", "number2<", "3n45"))
c("this is number 1", "hi mark", "number2<", "3n45")