Δημήτρης Πολιτικός e-mail: dimpolitik@gmail.com Τμήμα Τεχνολογίας Αλιείας – Υδατοκαλλιεργειών Τ.Ε.Ι. Δυτικής Ελλάδας (Μεσολόγγι) 1η Διάλεξη Εισαγωγή στην R Δημήτρης Πολιτικός e-mail: dimpolitik@gmail.com
Εγκατάσταση R https://cran.r-project.org/ base Binaries for base distribution. This is what you want to install R for the first time R-3.5.1 for Windows (32/64 bit) Download R 3.5.1 for Windows (62 megabytes, 32/64 bit) Installation and other instructions New features in this version
Εγκατάσταση RStudio https://www.rstudio.com/
Περιβάλλον της R Editor R Console
Αντικείμενα Δεδομένων 1. Βαθμωτά αντικείμενα (scalars) 2. Διανύσματα (vectors) 3. Πίνακες (matrices) 4. Πλαίσια δεδομένων (data frames) 5. Λίστες (lists) 6. Character Strings Scalar chr = 'this is a string'
Scalars Vectors Vectors with : and seq x <- 2 combine function (an ordered list of elements of the same type) x <- 2 y <- 3 z <- x+y x^2 sqrt(x) exp(x) v <- c(1,10,123,64,32,55) v/2 v^2 min(v) max(v) range(v) length(v) sum(v) mean(v) var(v) sort(v) sqrt(v) v[4] v[2]+2*v[3] v[2:5] v[3] <- 11; v[c(4,6)] v[v<=4 | v>60] id= which(v<20) Vectors with : and seq w <- 2:8 w <- seq(1,10) w <- seq(1,10,2) w <- seq(10,1,-2) w <- seq(-6,7) w <- seq(-2,2,0.3)
Συναρτήσεις για σύνοψη δεδομένων
Χρήσιμες Εντολές getwd() to find current working directory setwd() to change working directory Session-> Set working directory -> Choose Directory to change working directory dir() to list files in current working directory CtrL+L to clear console Session -> Restart R restart R Session -> ClearWorkplace to clear workplace # to comment lines in editor Run button: to run a specific line Source button: to run the whole script Tools-> install packages to install a package Ctrl + Z to undo !!! Ctrl + F to find something in the code
Matrices P <- matrix(c(2,4,1,5,7,3),nrow=2, ncol=3) class(P) dim(P) length(P) P[3,1] P[2,] P[,4] P[5]
Dataframes a <- c("male","female","male","female","male","male") b <- c(24,32,45,67,43,21) c <- c(181,167,178,170,175,2008) d <- c(81,55,75,74,78,95) df <- data.frame(SEX=a,AGE=b,HEIGHT=c,WEIGHT=d) df head(df,5);names(df);dim(df);mode(df); class(df);summary(df);str(df);length(df); df$HEIGHT; mean(df$WEIGHT); id<-which(df$SEX=“male”)
Lists MyList = list( "a"=1:3 , "b"=matrix(1:6,nrow=2) , "c"="Hello, world." ) MyList MyList$a MyList$a[2] The list structure is a generic vector in which components can be of different types, and named. li <- list(z, v, P, df) li; dim(li); size(li); li[2]; li[[2]], li[[2]][1]
Character Strings Data type R for storing sequences of characters 1. msg <- ‘Error’ 2. num_vec <- 1:5 names(num_vec) <- c('uno', 'dos', 'tres', 'cuatro', 'cinco') num_vec 3. dat <- read.csv(file = 'dataset.csv’) Data type R for storing sequences of characters 4. plot(x, y, main = 'Main Title', sub = 'Subtitle', xlab = ‘Year’, ylab = ‘Length', col = 'red', pch = 'x’) pi <- paste("The life of", pi) 5. str <- c('sun', 'sky', 'clouds')
dial_r1.r Εκπαιδευτικό Υλικό 1. Introduction-R.pdf στο eclass http://www.mayin.org/ajayshah/KB/R/index.html 2. R by example https://www.datacamp.com/courses/free-introduction-to-r 3. Datacamp 4. Coursera https://www.coursera.org/courses?query=r 1. Introduction-R.pdf στο eclass dial_r1.r