##Vytvoření vektoru---------------------------------------------------- c(1,2,3,4,5,6,7,8,9,10) c(1:10) x <- seq(1,10, by=2) rep(x, times=2) sample(x) sample(x, size=1) sample(x, size=5, replace=F) ##Dotazování na vector--------------------------------------------------- x <- c(5:15) length(x) head(x) tail(x, n=3) x[5] x > 10 which(x > 10) x[which(x > 10)] x[x > 10] x[-c(which(x > 10))] which(x > 10 | x < 8) which(x > 10 & x < 12) which.max(x) which.min(x) ##Faktory-------------------------------------------------------------------------------- factor(c("jablko", "malina", "jahoda", "orech", "malina")) f <- factor(c(1:5)) mean(f) factor(c(1, 0, 1, 0, 0), labels=c("ne", "ano")) ##Matice--------------------------------------------------------------------------------- x <- c(1:20) matrix(x, nrow=5, ncol=4) matrix(x, nrow=5, ncol=5) m <- matrix(x, nrow=5, ncol=4, byrow=TRUE) dim(m) m.t <- t(m) dim(m.t) ncol(m) nrow(m) sum(m) mean(m) colSums(m) rowSums(m) apply(m, 2, FUN=mean) x1 <- c(1:10) x2 <- c(10:1) cbind(x1, x2) rbind(x1, x2) ##Dotazovani na matici----------------------------------------- m[2,1] m[,1] m[2,] m[,c(1,3)] m[-3,c(1,3)] colSums(m[-3,c(1,3)]) colnames(m) <- letters[1:4] colnames(m) m[,"a"] ###Zminit co je to array – vicerozmerny vektor stejného typu array1 <- array(1:12, dim(2,3,2)) ###Data frame------------------------------------------------------- ##každý sloupec může nést jiný typ dat X matrix ##slopce se dají pojmenovat a volat pres $ df <- as.data.frame(m) class(df) colnames(df) df[,"sloupec2"] df[, c("sloupec2", " sloupec3")] df$sloupec3 m$sloupec1 df <- data.frame(prom1=1:5, char1=letters[1:5], empty=rep(NA, 5), logic = c(1:5)>3) df class(df) class(df[,1]) class(df[,2]) class(df[,3]) class(df[,4]) df$empty <- rnorm(5) df class(df$empty) ###pridani noveho sloupce df$new <- factor(sample(0:1, 5, replace = T), labels = c("ne", "ano")) ##List-------------------------------------------------------------------------------------------- my.list <- list(moje.matice = m, muj.df = df, novy.vec = 1:100) my.list my.list[[1]] my.list$moje.matice my.list[1] class(my.list[1]) class(my.list[[1]]) my.list$NEW <- rnorm(1:100) str(my.list) my.list$muj.df my.list$muj.df[,2] my.list$muj.df$char1 my.list$muj.df$char1[3] ###empty list my.list2 <- list() my.list2 my.list2[[1]] <- rnorm(5) my.list2[["druhy"]] <- 1:5 my.list2 unlist(my.list2) names(my.list2) names(my.list2)[1] <- "prvni" unlist(my.list2) my.list2[["treti"]] <- LETTERS[1:5] unlist(my.list2) tab.from.list <- do.call(rbind, my.list2) tab.from.list <- as.data.frame(do.call(rbind, my.list2)) ##export import dat-------------------------------------------------------------------------------------- data("mtcars") mtcars write.table(mtcars, file="Auta.txt", sep = "\t", dec=".") write.csv write.csv2 ##import dat moje.data <- read.delim("Auta2.txt", header=T, row.names = 1) moje.data class(moje.data) class(moje.data$Klas) class(moje.data$Klas2) moje.data <- read.delim("./Moje_data/Auta2.txt", header=T, row.names = 1) ###Obrazky moje.data$Klas2[moje.data$Klas2 == 0] <- 2 plot(moje.data$hp, moje.data$qsec, pch=16, col= c("blue", "red")[moje.data$Klas2])