82 lines
2.7 KiB
R
Raw Normal View History

2020-08-14 15:08:59 +02:00
###########################################################################
###########################################################################
### ###
### UPORABNE FUNKCIJE ###
### ###
###########################################################################
###########################################################################
# Funkcija za prilagojeno zaokrozevanje stevil
round_custom = function(x) {
x <- round(x, 1)
x <- case_when(
x >= 4.9 ~ 5,
x >= 4.1 & x <= 4.8 ~ 4,
x >= 3.1 & x <= 4.0 ~ 3,
x >= 2.1 & x <= 3.0 ~ 2,
x >= 1.3 & x <= 2.0 ~ 1,
TRUE ~ NA_real_
)
return(x)
}
# Priprava podatkov za graf
prep.dat <- function(df, variable) {
if(is.null(dim(df)))
df <- round_custom(mean(df[variable], na.rm = TRUE) / 4 * 5)
else
df <- round_custom(rowMeans(df[, variable], na.rm = TRUE) / 4 * 5)
df <- data.frame(prop.table(table(df)) * 100)
names(df)[1] <- "Ocena"
names(df)[2] <- "Percentage"
return(df)
}
# Menjava znakov za imena PDF datotek grafov
replace.characters <- function(vector.to.tidy){
vector.to.tidy <- gsub("-|\\.", "", vector.to.tidy)
vector.to.tidy <- gsub(" ", "", vector.to.tidy)
vector.to.tidy <- gsub("\"", "", vector.to.tidy)
vector.to.tidy <- gsub("\u010C", "C", vector.to.tidy)
vector.to.tidy <- gsub("\u0160", "S", vector.to.tidy)
vector.to.tidy <- gsub("\u017D", "Z", vector.to.tidy)
vector.to.tidy <- gsub("\u010D", "c", vector.to.tidy)
vector.to.tidy <- gsub("\u0161", "s", vector.to.tidy)
vector.to.tidy <- gsub("\u017E", "z", vector.to.tidy)
vector.to.tidy <- gsub("\u017E", "z", vector.to.tidy)
vector.to.tidy <- gsub("[(\\...\\,)]", "", vector.to.tidy)
return(vector.to.tidy)
}
tidy.c.space <- function(vector.to.tidy){
vector.to.tidy <- gsub("\u010C", "\u010C ", vector.to.tidy)
vector.to.tidy <- gsub("\u010D", "\u010D", vector.to.tidy)
return(vector.to.tidy)
}
# Dodajanje oznak, ki bodo prikazane v grafu
labScore <- function(df, labelord) {
df$label <- case_when(
df$Ocena == 1 ~ "Nesprejemljivo",
df$Ocena == 2 ~ "Nezadostno",
df$Ocena == 3 ~ "Povpre\u010Dno",
df$Ocena == 4 ~ "Dobro",
df$Ocena == 5 ~ "Odli\u010Dno",
TRUE ~ NA_character_
)
# Imena po katerih bomo razvrstili stolpce v grafih
df$name <- labelord
return(df)
}
# Factor spremenljivke v stringe ter dodajanje imen oddelkov
factor_to_string <- function(df, ime_dejavnika, ime_oddelka){
tmp <- df[df$name == ime_dejavnika, ]
i <- sapply(tmp, is.factor)
tmp[i] <- lapply(tmp[i], as.character)
tmp$name <- ime_oddelka
return(tmp)
}