Vytvorme si údaje o výnosoch,výdavkoch a zisku firmy.
Spojíme ich do tabuľky.
Pridáme si do tabuľky dodatočný stĺpec, ktorý obsahuje boolovské hodnoty, či firma generuje zisk alebo nie.
Teraz si vyskúšame pridať do našej tabuľky riadok.
novy.riadok <- data.frame(Výnosy = 300, Výdavky = 480, Zisk = -180, Generuje_zisk = FALSE)
# Append
firma <- rbind(firma, novy.riadok)
print(firma)
library(knitr)
library(kableExtra)
kable(
firma,
# format,
digits = 2,
# row.names = NA,
# col.names = NA,
align=c("l","c","l","r"),
caption = "Toto je tabuľka"
# label = NULL,
# format.args = list(),
# escape = TRUE,
# ...
) %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"),
full_width = FALSE,
position = "center")
Výnosy | Výdavky | Zisk | Generuje_zisk |
---|---|---|---|
100 | 170 | -70 | FALSE |
150 | 120 | 30 | TRUE |
400 | 160 | 240 | TRUE |
300 | 480 | -180 | FALSE |
firma %>%
filter(Výnosy > 150) %>%
arrange(desc(Výnosy)) %>%
kable %>%
kable_styling(
bootstrap_options = c("striped", "hover", "condensed", "responsive"),
full_width = FALSE,
position = "center"
)
Výnosy | Výdavky | Zisk | Generuje_zisk |
---|---|---|---|
400 | 160 | 240 | TRUE |
300 | 480 | -180 | FALSE |
firma %>%
group_by(Generuje_zisk) %>%
summarise(
Priem.Výnos = mean(Výnosy),
count = n()
) %>%
kable(
caption = "Priemerné Výnosy podľa premennej Generuje_zisk",
col.names = c("Generuje zisk", "Priemer Výnos", "Počet"),
align = "c"
) %>%
kable_styling(
bootstrap_options = c("striped", "hover", "condensed", "responsive"),
full_width = FALSE,
position = "center"
)
Generuje zisk | Priemer Výnos | Počet |
---|---|---|
FALSE | 200 | 2 |
TRUE | 275 | 2 |
firma %>%
mutate(
Kategória = case_when(
Zisk > 100 ~ "Výborný výsledok",
Zisk > 0 ~ "Ziskový",
Zisk >- 100 ~ "Strata",
TRUE ~ "Veľká strata"
),
Percento_zisku = round((Zisk / Výdavky) * 100, 1) # tu je tvoja "druhá" premenná
) %>%
kable() %>%
kable_styling(
bootstrap_options = c("striped", "hover", "condensed", "responsive"),
full_width = FALSE,
position = "center"
)
Výnosy | Výdavky | Zisk | Generuje_zisk | Kategória | Percento_zisku |
---|---|---|---|---|---|
100 | 170 | -70 | FALSE | Strata | -41.2 |
150 | 120 | 30 | TRUE | Ziskový | 25.0 |
400 | 160 | 240 | TRUE | Výborný výsledok | 150.0 |
300 | 480 | -180 | FALSE | Veľká strata | -37.5 |
Vytvoríme si stĺpcový graf, pomocou ktorého porovnáme zisky jednotlivých firiem.
ggplot(firma, aes(x = factor(1:nrow(firma)), y = Zisk, fill = Generuje_zisk)) +
geom_bar(stat = "identity") +
labs(
title = "Zisk jednotlivých firiem",
x = "Firma",
y = "Zisk (€)",
fill = "Generuje zisk"
) +
theme_minimal()