# 1. X <- read.csv(file = "oil.csv", header = TRUE, sep = ";", dec = ",") names(X) str(X) summary(X) X$datum <- ISOdate(X$rok, X$mesic, X$den) WTI <- X[X$typ == "WTI", ] BRENT <- X[X$typ == "BRENT", ] plot(WTI$datum, WTI$cena, type = "l", col = "red", xlab = "datum", ylab = "cena", ylim = c(0, 145)) lines(BRENT$datum, BRENT$cena, col = "blue") legend("topleft", legend = c("WTI", "BRENT"), col = c("red", "blue"), lty = 1) BRENT.r <- tapply(BRENT$cena, BRENT$rok, mean) BRENT.r <- data.frame(cena = BRENT.r) BRENT.r$rok <- as.numeric(rownames(BRENT.r)) BRENT.r$typ <- factor("BRENT") WTI.r <- tapply(WTI$cena, WTI$rok, mean) WTI.r <- data.frame(cena = WTI.r) WTI.r$rok <- as.numeric(rownames(WTI.r)) WTI.r$typ <- factor("WTI") plot(WTI.r$rok, WTI.r$cena, type = "l", col = "red", xlab = "rok", ylab = "prumerna rocni cena", ylim = c(0, 110)) lines(BRENT.r$rok, BRENT.r$cena, col = "blue") legend("topleft", legend = c("WTI", "BRENT"), col = c("red", "blue"), lty = 1) Y <- merge(WTI.r, BRENT.r, all = TRUE) Z <- tapply(Y$cena, list(Y$rok, Y$typ), mean) barplot(t(Z), beside = TRUE, col = c("red", "blue"), las = 2, xlab = "rok", ylab = "prumerna rocni cena") legend("topleft", legend = c("WTI", "BRENT"), col = c("red", "blue"), lty = 1) fn1 <- function (x) { return(x[2] - x[1]) } W <- apply(Z, 1, fn1) barvy <- rep("green3", length(W)) barvy[W < 0] <- "yellow" barplot(W, names.arg = rownames(W), las = 2, col = barvy, xlab = "rok", ylab = "rozdil prumerne rocni ceny BRENT - WTI ") # 2. X <- read.csv(file = "titanic1.csv", header = TRUE, sep = ",", dec = ".") X$pclass <- factor(X$pclass, levels = c(1, 2, 3), labels = c("class 1", "class 2", "class 3")) X$survived <- factor(X$survived, levels = c(0, 1), labels = c("no", "yes")) levels(X$embarked) <- c("Cherbourg", "Queenstown", "Southampton") names(X) str(X) summary(X) (T1 <- tapply(X$age, list(X$pclass, X$sex), length)) (T2 <- tapply(X$survived == "yes", list(X$pclass, X$sex), sum)) (T3 <- round(T2 / T1 * 100, digits = 1)) apply(T1, 2, sum) apply(T1, 1, sum) barplot(t(T1), col = c("red", "cyan"), xlab = "cestovni trida", ylab = "pocet cestujicich") legend("top", legend = levels(X$sex), fill = c("red", "cyan")) barplot(t(T2), col = c("red", "cyan"), xlab = "cestovni trida", ylab = "pocet prezivsich cestujicich") legend("top", legend = levels(X$sex), fill = c("red", "cyan")) (T4 <- round(tapply(X$age, list(X$pclass, X$sex), mean, na.rm = TRUE), digits = 1)) Y <- X[X$survived == "yes", ] (T5 <- round(tapply(Y$age, list(Y$pclass, Y$sex), mean, na.rm = TRUE), digits = 1)) (T6 <- round(tapply(X$fare, list(X$pclass, X$sex), mean, na.rm = TRUE), digits = 1)) (T7 <- round(tapply(X$fare, list(X$pclass, X$sex), max, na.rm = TRUE), digits = 1)) (T8 <- table(X$survived, X$sex)) (sance.pro.zeny <- T8[2, 1] / T8[1, 1]) (sance.pro.muze <- T8[2, 2] / T8[1, 2]) (pomer.sanci <- sance.pro.zeny / sance.pro.muze)