-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclean-special-characters.R
35 lines (29 loc) · 1.22 KB
/
clean-special-characters.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
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
library(tidyverse)
# Functions. -------------------------------------------------------------------
replace_all <- function(df, pattern, replacement) {
char <- vapply(df, function(x) is.factor(x) || is.character(x), logical(1))
df[char] <- lapply(df[char], str_replace_all, pattern, replacement)
df
}
# Main. ------------------------------------------------------------------------
lf <- list.files("./data-raw")
lf_slo <- str_subset(lf, "^SLO")
lf_slo
for (f in lf_slo) {
df <- read_csv2(paste0("./data-raw/", f),
locale = readr::locale(encoding = "cp1250"))
colnames(df) <- str_replace(colnames(df), "È", "C")
colnames(df) <- str_replace(colnames(df), "è", "c")
colnames(df) <- str_replace(colnames(df), "Š", "S")
colnames(df) <- str_replace(colnames(df), "š", "s")
colnames(df) <- str_replace(colnames(df), "Ž", "Z")
colnames(df) <- str_replace(colnames(df), "ž", "z")
df <- replace_all(df, "È", "C")
df <- replace_all(df, "è", "c")
df <- replace_all(df, "Š", "S")
df <- replace_all(df, "š", "s")
df <- replace_all(df, "Ž", "Z")
df <- replace_all(df, "ž", "z")
write_csv2(df, paste0("./data-no-special-characters/", f))
}