############################################################### ######### Introduction to R ######### ##### Prof. Ilaria Benedetti ##### ############################################################### # (#) to provide comments in green colour # --------------------------- first operations with R ----------------------- # R as calculator 2+2 4*13 (8-4)/4 6^2 # -> control+enter (windows) oppure cmd +enter(mac) # some function included in R .... # square root sqrt(16) #absolute value: abs(-10) #logaritm base e, natural logarithms, log(10) #logaritm base 10: log10(10) ------------------------------------------------------------ # R works with objects # the arrow assingns the number to the object called " a " a <- 8 print(a) # print function shows the content of the obj a # olternative way to create an object a = 8 8 -> a a # be carefull with the name !!!! R rewrite objecs with the same name a=6 # now object a contains n.6 a # Type of data ------------------------------------------------------------ # 1- Logical (TRUE, FALSE) (qualitative data) # 2- Numeric (12.3, 5, 999 ) (quantitative data) # 3- Character 'a' , '"good", "TRUE", '23.4' (qualitative data) # 1- Logical (TRUE, FALSE) v <- TRUE print(class(v)) # class function define type of data v # 2- Numeric (12.3, 5, 999 ) v <- 23.5 # full stop as a decimal separator print(class(v)) # 3- Character: qualitative data (eg. eductional level, type of company, etc....) v <- "TRUE" # please use the "" (quotes) to create charact objects print(class(v)) # example # 1 - create an object called "height" wich contains the value 180 (cm) # 2- create an object called "civilstatus" wich contains the value married # Ask to R the nature of the obj is.numeric(height) is.numeric(civilstatus) is.character(civilstatus) mode(height) mode(civilstatus) # Workspace: environemnt with the create obj -------------- ls() # all the obj in environment rm(a) # remove obj called "a" # rm(list=ls()) # ------------------------------------------------------------------------------------- # type of object: # Vectors # Matrices # Factors # Data Frames # VECTORS #The simplest of these objects is the vector object and there are six data types of these atomic vectors, also termed as six classes of vectors. The other R-Objects are built upon the atomic vectors. # vector height includes the heights of 4 individuals (in mt.) height=c(1.87,1.75,1.68,1.9) # height # order the vector in ascending or descending orde (by default is ascending) sort(height) # descending --> specify: decreasing=T sort(height,decreasing=T) # Character (character, logical values of the vectors ) # create a vector includind the edu level of three individuals educational_level<-c("primary edu", "secondary edu", "tertiary edu") educational_level mode(educational_level) # create a logial vector in which the value is equal to TRUE if a student is "in corso", FALSE is a student "is NOT in corso" incorso<-c(TRUE,FALSE,TRUE,TRUE) #logical incorso mode(incorso) # operations with vectors #n. of units included in the vector --> funz: lenght length(height) # four units # exercise to do # In a sample of 10 students, the waiting times at the canteen before being served have been collected # observed values here: # 3 29 0 0 25 60 48 16 25 29 # 1- create the vector # 2- ask Rfor the nature # 3- how many units? # DATAFRAME --> dataset # 1- set your working directory (w.d.) setwd("~/Desktop/dataset") #set your working directory (your folder in which you stored data) #change directory Session>Set working Directory # vedere working directory getwd() # format of the dataframe # CSV # excel (eg. xlxs ) # CSV format library(readr) # load this package! my_data <- read_csv("wage.csv") # EXCEL FORMAT library(readxl) # load this package! mydata <- read_excel("wage.xlsx") # how to see the data???? #show fist 6 lines head(mydata) #show last 6 lines tail(mydata) # info: class(), colnames(), rownames(), dim() class(mydata) colnames(mydata) rownames(mydata) dim(mydata) # structure of the dataset str(mydata) #description of the data summary(mydata) # levels of variable Contract mydata$contract <-as.factor(mydata$contract) #declare variable as factor levels(mydata$contract) #modalities of the variable